工作中用到json格式字符串需要解析,所以总结了以下几种情况供以后使用。
1.简单的json格式:
String str = "{"
+"\"ticket\":\"xxxxxxxx\","
+"\"result\":\"success\","
+"\"userinfo\":{\"name\":\"SMP\","
+"\"ip\":\"192.168.0.1\"}"
+"}";
解析代码如下:
JSONObject json = new JSONObject(str);
JSONObject userinfo = json.getJSONObject("userinfo");
System.out.println(json.getString("result"));
System.out.println(userinfo.getString("name"));
2.json对象带有数组的:
String str = "{"
+"\"ticket\":\"xxxxxxxx\","
+"\"result\":\"success\","
+"\"userinfo\":[{\"name\":\"SMP\","
+"