import org.json.JSONArray;import org.json.JSONException;
//因为json有两个包,第一种:通过org.json包
public class test
{ public static void main(String[] args) throws JSONException { String str = "[{'id':5,'name':'张三'},{'id':6,'name':'李四'}]"; JSONArray jsonArray = null; jsonArray = new JSONArray(str); System.out.println(jsonArray.getJSONObject(0).get("name")); }}
import net.sf.json.JSONFunction;
import net.sf.json.JSONObject;
//因为json有两个包,第二种:通过net.sf.json包
class test2
{ public static void main(String[] args) throws JSONException { String str = "{'array':[{'id':5,'name':'张三'},{'id':6,'name':'李四'}]}";JSONObject jsonobj=JSONObject.fromObject(json);//将字符串转化成json对象
JSONArray jsonArray = null; jsonArray = jsonobj.getJSONArray("array");//获取数组 System.out.println(jsonArray.getJSONObject(0).get("name")); }}
本文介绍如何使用Java中的org.json及net.sf.json两个库来解析包含中文字符的JSON数组,并展示了两种不同的实现方式。
3801

被折叠的 条评论
为什么被折叠?



