0

how to directly parse JSONdata using JSONObject in java as my JSON Data don't have any 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 code

 jsonObject = new JSONObject(results);


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

                jsonObject=jsonObject.getJSONObject();


                j = jsonObject.getString("type");

              //  mainActivityModel.setId();

            }

2 Answers2

0

You json is incorrect.

{ 
"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" }
}

You can parse json by this.

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

To get json object you will call getJSONObject() and to get String you will call getString()

And if you need to parse a json Array

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

0

go through this tutorial it can help you in understanding json parsing http://www.technotalkative.com/android-json-parsing/

44kksharma
  • 2,740
  • 27
  • 32