如何在java中使用JSONObject直接解析jsondata?

时间:2017-01-14 08:20:41

标签: java android json

如何在java中使用JSONObject直接解析JSONdata,因为我的JSON数据没有任何JSONArray。

JSONData: -

{{
"id": 481,
"date": "2016-12-30T13:56:10",
"date_gmt": "2016-12-30T13:56:10",
"guid": {
"rendered": "http://www.mytrendin.com/wp-content/uploads/2016/12/read-           1710011_1280.jpg"
},
"modified": "2016-12-30T13:56:20",
"modified_gmt": "2016-12-30T13:56:20",
"slug": "read-1710011_1280",
"type": "attachment",
"link": "http://www.mytrendin.com/increase-child-development/read-1710011_1280/",
"title": {
"rendered": "child development"
}}

java代码

 jsonObject = new JSONObject(results);


            for(i=0;i<jsonObject.length();i++){

                jsonObject=jsonObject.getJSONObject();


                j = jsonObject.getString("type");

              //  mainActivityModel.setId();

            }

2 个答案:

答案 0 :(得分:0)

json不正确

{ 
"id": 481, 
"date": "2016-12-30T13:56:10", 
"date_gmt": "2016-12-30T13:56:10", 
"guid": { "rendered": "http://www.mytrendin.com/wp-content/uploads/2016/12/read- 1710011_1280.jpg" },
 "modified": "2016-12-30T13:56:20", "modified_gmt": "2016-12-30T13:56:20", 
"slug": "read-1710011_1280",
 "type": "attachment",
 "link": "http://www.mytrendin.com/increase-child-development/read-1710011_1280/", "title": { "rendered": "child development" }
}

你可以解析json。

JSONObject obj = new JSONObject(result);
JSONObject guid=obj.getJSONObject("guid");

要获取json对象,您将调用getJSONObject()并获取String,您将调用getString()

如果你需要解析一个json数组

JSONArray json_arr = new JSONArray(results); for(i=0;i<json_arr.length();i++){ JSONObject jsonObject=json_arr.getJSONObject(i); }

答案 1 :(得分:0)

通过本教程,它可以帮助您理解json解析 http://www.technotalkative.com/android-json-parsing/