工作中记录获取Json数组的坑


// 需要导入包 import net.sf.json.JSONObject;
// json形式的字符串
String str = "{'company':'Trump','userInfo':[{'id':1000,'name':'唐纳德·特朗普','age':'78','hobby':'发推特'}]}";

// 把String转成JSONObject形式
JSONObject jsonStr = JSONObject.fromObject(str);

// 从jsonStr中获取JSONArray数组
JSONArray userInfo = JSONArray.fromObject(jsonStr.get("userInfo"));

// 获取第一,多个的话可以遍历
JSONObject o = (JSONObject) userInfo.get(0);

System.out.println("姓名:" + o.get("name") + ",年龄:" + o.get("age")+",爱好:" + o.get("hobby")+"。");
原文地址:https://www.cnblogs.com/nginxTest/p/13038611.html