直接给代码:
- packagefastJson.test;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importcom.alibaba.fastjson.JSON;
- importcom.alibaba.fastjson.JSONArray;
- publicclassFastJsonTest1
- {
- /**
- *数组转json格式字符串
- */
- voidarray2Json(){
- String[]arr={"bill","green","maks","jim"};
- StringjsonText=JSON.toJSONString(arr,true);
- System.out.println("array2Json()方法:jsonText=="+jsonText);
- //输出结果:jsonText==["bill","green","maks","jim"]
- }
- *json格式字符串转数组
- voidjson2Array(){
- StringjsonText="[\"bill\",\"green\",\"maks\",\"jim\"]";
- JSONArrayjsonArr=JSON.parseArray(jsonText);
- System.out.println("json2Array()方法:jsonArr=="+jsonArr);
- //输出结果:jsonArr==["bill",153); font-weight:bold; background-color:inherit">voidarray2Json2(){
- Useruser1=newUser("P001","TOM",16);
- Useruser2=newUser("P002","JACKSON",0); background-color:inherit">21);
- Useruser3=newUser("P003","MARTIN",0); background-color:inherit">20);
- User[]userArr={user1,user2,user3};
- StringjsonText=JSON.toJSONString(userArr,153); font-weight:bold; background-color:inherit">true);
- System.out.println("array2Json2()方法:jsonText=="+jsonText);
- //输出结果:jsonText==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]
- }
- /**
- *json格式字符串转数组
- */
- voidjson2Array2(){
- StringjsonText="[{\"age\":16,\"id\":\"P001\",\"name\":\"TOM\"},{\"age\":21,\"id\":\"P002\",\"name\":\"JACKSON\"},{\"age\":20,\"id\":\"P003\",\"name\":\"MARTIN\"}]";
- JSONArrayjsonArr=JSON.parseArray(jsonText);
- System.out.println("json2Array2()方法:jsonArr=="+jsonArr);
- //输出结果:jsonArr==[{"age":16,0); background-color:inherit">*list集合转json格式字符串
- voidlist2Json(){
- Listlist=newArrayList();
- newUser("L001",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> list.add(user1);
- Useruser2=newUser("L002",0); background-color:inherit">21);
- list.add(user2);
- newUser("L003",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> list.add(user3);
- StringjsonText=JSON.toJSONString(list,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> System.out.println("list2Json()方法:jsonText=="+jsonText);
- voidlist2Json2(){
- Addressaddress1=newAddress("广东省","深圳市","科苑南路","580053");
- Useruser1=16,address1);
- list.add(user1);
- Addressaddress2=newAddress("江西省","南昌市","阳明路","330004");
- 21,address2);
- Addressaddress3=newAddress("陕西省","西安市","长安南路","710114");
- Useruser3=20,address3);
- list.add(user3);
- StringjsonText=JSON.toJSONString(list,248)"> System.out.println("list2Json2()方法:jsonText=="+jsonText);
- //输出结果:jsonText==[{"address":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"age":16,{"address":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"age":21,{"address":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"},"age":20,"name":"MARTIN"}]
- *map转json格式字符串
- voidmap2Json(){
- Mapmap=newHashMap();
- Addressaddress1="580053");
- map.put("address1",address1);
- map.put("address2",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> Addressaddress3="710114");
- map.put("address3",address3);
- StringjsonText=JSON.toJSONString(map,248)"> System.out.println("map2Json()方法:jsonText=="+jsonText);
- //输出结果:jsonText=={"address1":{"city":"深圳市","address2":{"city":"南昌市","address3":{"city":"西安市","street":"长安南路"}}
- staticvoidmain(String[]args)
- {
- FastJsonTest1test=newFastJsonTest1();
- //数组转json格式字符串
- test.array2Json();
- //json格式字符串转数组
- test.json2Array();
- test.array2Json2();
- test.json2Array2();
- //list集合转json格式字符串
- test.list2Json();
- //list集合转json格式字符串
- test.list2Json2();
- //map转json格式字符串
- test.map2Json();
- }
控制台输出结果:
[html]
copy