java 解析json字符串

如果转载我的这篇文章请注明出处,谢谢!

最近工作中,需要解析json格式的字符串,恰好有个例子,感觉不错,拿来分享。

 运行这个类需要加载jar包:ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar、jsoup-1.6.1.jar、commons-beanutils-1.8.0.jar、commons-collections.jar、commons-lang-2.4.jar、commons-logging-1.1.1.jar。

commons-beanutils-1.8.0.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean 
commons-collections.jar 不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap 
commons-lang-2.4.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException 
commons-logging-1.1.1.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
ezmorph-1.0.6.jar不加这个包 
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 
json-lib-2.4-jdk15.jar不加这个包 
java.lang.NoClassDefFoundError: net/sf/json/JSONObject 
相应jar包可到网上下载,也可以用下面提供的!

 1 package src.util;

 2 import java.util.ArrayList;

 3 import java.util.HashMap;

 4 import java.util.Iterator;

 5 import java.util.List;

 6 import java.util.Map;

 7 import net.sf.json.JSONArray;

 8 import net.sf.json.JSONObject;

 9 public class TestJson

10 {

11     // json字符串转

12     private static String diskListContent =

13         "[{\"n1\":\"asd\",\"n2\":22,\"n3\":\"45.40GB\"," + "\"n4\":\"qwerty\",\"n5\":\"asd\",}," + "{\"n1\":\"local\","

14             + "\"n2\":1,\"n3\":\"279.40GB\",\"n4\":\"ST3300656SS\",\"n5\":\"\\/devm\\/d0\"}]";

15     /***

16      * json字符串转java List

17      * @param rsContent

18      * @return

19      * @throws Exception

20      */

21     private static List<Map<String, String>> jsonStringToList(String rsContent) throws Exception

22     {

23         JSONArray arry = JSONArray.fromObject(rsContent);

24         System.out.println("json字符串内容如下");

25         System.out.println(arry);

26         List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();

27         for (int i = 0; i < arry.size(); i++)

28         {

29             JSONObject jsonObject = arry.getJSONObject(i);

30             Map<String, String> map = new HashMap<String, String>();

31             for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();)

32             {

33                 String key = (String) iter.next();

34                 String value = jsonObject.get(key).toString();

35                 map.put(key, value);

36             }

37             rsList.add(map);

38         }

39         return rsList;

40     }

41     /**

42      * @param args

43      * @throws Exception

44      */

45     public static void main(String[] args) throws Exception

46     {

47         List<Map<String, String>> list1 = jsonStringToList(diskListContent);

48         System.out.println("json字符串成map");

49         for (Map<String, String> m : list1)

50         {

51             System.out.println(m);

52         }

53         System.out.println("map转换成json字符串");

54         for (Map<String, String> m : list1)

55         {

56             JSONArray jsonArray = JSONArray.fromObject(m);

57             System.out.println(jsonArray.toString());

58         }

59         System.out.println("list转换成json字符串");

60         JSONArray jsonArray2 = JSONArray.fromObject(list1);

61         System.out.println(jsonArray2.toString());

62     }

63 }

 

 

你可能感兴趣的:(java)