首页 > 代码库 > json原生解析jsonObject和jsonArray

json原生解析jsonObject和jsonArray

public void jsonParse() throws JSONException{
        String jsonStr = "{‘name‘:‘zhangsan‘,‘age‘:‘19‘}";
        JSONObject jobj = new JSONObject(jsonStr);
        String name = jobj.getString("name");
//        String age = jobj.getString("age");
        int age = jobj.getInt("age");
        System.out.println(name+"age = "+age);
        String jsonArr = "[{‘name‘:‘zhangsan‘,‘age‘:‘19‘},{‘name‘:‘lisi‘,‘age‘:‘29‘}]";
        JSONArray jarr = new JSONArray(jsonArr);
        for(int i = 0;i<jarr.length();i++){
            JSONObject jsonObject = jarr.getJSONObject(i);
            name = jsonObject.getString("name");
            System.out.println(name);
        }
    }

 

json原生解析jsonObject和jsonArray