java获取json数组格式中的值

来源:互联网 发布:登陆淘宝显示网络崩溃 编辑:程序博客网 时间:2024/06/06 17:45
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.JSONArray;  
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"));
    }
 
}



原创粉丝点击