converting json response (from wordpress REST API version-2 Beta) to json array in Android Studio - android

Below is the json response from wordpress:
[{
"id": 711,
"date": "2015-09-28T01:00:03",
"date_gmt": "2015-09-27T19:30:03",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=711"
},
"modified": "2015-09-28T01:51:52",
"modified_gmt": "2015-09-27T20:21:52",
"slug": "why-only-and-only-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-only-and-only-realcake\/",
"title": {
"rendered": "Why only and only RealCake"
},
"content": {
"rendered": "<p><img class=\"size-full wp-image-725 aligncenter\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg\" alt=\"REALCAKE BAKERY IN GURGAON R.\" width=\"940\" height=\"225\" srcset=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg 940w,
},
"excerpt": {
"rendered": ""
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [37, 35, 1],
"tags": [],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/711"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}, {
"id": 420,
"date": "2015-01-01T18:00:07",
"date_gmt": "2015-01-01T12:30:07",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=420"
},
"modified": "2015-08-04T23:29:55",
"modified_gmt": "2015-08-04T17:59:55",
"slug": "why-choose-muffins-cakes-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-choose-muffins-cakes-realcake\/",
"title": {
"rendered": "Why Choose Muffins & Cakes from RealCake Bakery?"
},
"content": {
"rendered": "<div id=\"attachment_423\" style=\"width: 559px\" class=\"wp-caption aligncenter\"><img class=\" wp-image-423\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/03\/Regular-muffins-e1425386051181-300x148.png\"
},
"excerpt": {
"rendered": "<p>. Feel free to write to us or call us if […]<\/p>\n"
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [35],
"tags": [44, 41, 46, 40, 45, 42, 38, 39, 43],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/420"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}]
==>> I am using the code below to get JSONArray:
Response response = client.newCall(request).execute();
//here I am getting the json response(mentioned above) from wordpress rest api
String json = response.body().string();
JSONObject object = new JSONObject(json);
//Below code is giving me null array as I dont have "Array name" in my json response.
JSONArray array = object.optJSONArray("Name of Array in json response");
Please help me get JSONArray out of json response.

Looking at your JSON string, it seems that the entire response body is a JSON-Array, so you can simply parse it directly into a instance of JSONArray.
This is how you can do it:
//get your JSON body-string like you do
String json = response.body().string();
//parse it directly into JSONArray
JSONArray array = new JSONArray(json);
//do whatever you want with the array...
I hope this helps you.

Related

How to pass object of json to other activity

I have this json
{
"results": [{
"gender": "male",
"name": {
"title": "mr",
"first": "isidro",
"last": "nogueira"
},
"location": {
"street": "3663 rua amazonas ",
"city": "mesquita",
"state": "rio de janeiro",
"postcode": 27744
},
"email": "isidro.nogueira#example.com",
"login": {
"username": "orangegoose315",
"password": "redalert",
"salt": "Dd3U7U03",
"md5": "325f1e8753222facba6ee63a5e2451de",
"sha1": "a63a0c544a956e235330b0ed76d11d3ad17c7979",
"sha256": "ddf18ffff89684617b2c7b8c5c175b9570cb6005916d9b4f3face802bd4f13a1"
},
"dob": "1967-04-23 04:58:32",
"registered": "2016-05-08 01:56:44",
"phone": "(11) 8368-8583",
"cell": "(17) 9743-3074",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/57.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/57.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/57.jpg"
},
"nat": "BR"
}],
"info": {
"seed": "5b3dff5c25caaf7f",
"results": 1,
"page": 1,
"version": "1.1"
}
}
This data is of just one user. I have 50 of them.
I am still in learning stage and what I have done is that I have successfully listed all user in an activity. Now what I want to do is when I click on a user it should show it's related details in next activity...
List Users->Select Users->Show selected user details
I still don't know how to send data to other activity and display it.

RetroFit, Many to many relationships

I have a json response that looks somewhat like this.
{
"id": 1,
"username": "user",
"email": "generic user#gmail.com",
"name": "harshvardhan",
"phone_no": 2147483647,
"created_at": "2016-06-27 12:23:25",
"updated_at": "2016-06-27 12:23:25",
"hash": "",
"groups": [
{
"id": 37,
"title": "aspernatur-et-rem-quos-eius-voluptatem-eveniet-aut",
"descr": "33",
"identifier": "5771545383a2d",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 37,
"roles": "admin"
}
},
{
"id": 67,
"title": "quibusdam-voluptas-non-facere-nihil",
"descr": "194",
"identifier": "57715453b8d80",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 67,
"roles": "admin"
}
},
{
"id": 161,
"title": "libero-id-ipsa-beatae-aut",
"descr": "59",
"identifier": "5771545469db4",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 161,
"roles": "admin"
}
},
{
"id": 198,
"title": "iure-ad-sunt-id-delectus-laboriosam-quo",
"descr": "150",
"identifier": "57715454a9d15",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 198,
"roles": "admin"
}
}
]
}
There is a many to many relationship between users and groups.
I am using retrofit with the GsonConverterFactory. On response, how can I parse this information into objects?
I tried making a User class , and a group class, then I put an array of groups as a member of the user class.
But when I access User.getGroups().get(1).getID , it returns 0.
However, User.getGroups().size() does return 4 (the correct number in this case)
How can I achieve this in retrofit?
A practical tool to parse JSON correctly with retrofit can be to generate the classes with http://www.jsonschema2pojo.org
You just have to put the example JSON, the resource type in JSON and put the desired annotation style (GSON, Jackson, ..).
Finally click on the preview button and you can see the results

How to make a pojo class for this type of json data, because in this category and subcategory are combine relation is only through a id

In this json my category and subcategory are combine, how can i arse and how to set in pojo class, i wan't to set in expandable list view, please help
{
"product_categories": [{
"id": 24,
"name": "Air Cooled",
"slug": "air-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 4
}, {
"id": 33,
"name": "Belt Driven",
"slug": "bet-driven",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 25,
"name": "CNC LATHE SPINDLE",
"slug": "cnclathespindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 28,
"name": "CNC MILLING SPINDLE",
"slug": "cncmillingspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 9
}, {
"id": 29,
"name": "Motorised",
"slug": "singles",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 0
}, {
"id": 21,
"name": "MOTORIZED SPINDLE",
"slug": "motorizedspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 11
}, {
"id": 30,
"name": "Water Cooled",
"slug": "water-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 7
}, {
"id": 67,
"name": "Wood Acrylic Engraving",
"slug": "wood-acrylic-engraving",
"parent": 21,
"description": "",
"display": "products",
"image": "",
"count": 0
}]
}
In this json my category and subcategory are combine, how can i arse and how to set in pojo class, i wan't to set in expandable list view, please help
You have to parse the json response and store the different categories in different arraylist and based on the user selection of your render position you can use the selected arraylist.
You can do this by parsing the json response like this
JSONObject jsonObject = new JSONObject(response); // response is the json string that you are gettting from server
JSONArray jsonArray = jsonObject.getJSONObject("product_categories");
ArrayList<Categorydisplay> catDisList = new ArrayList<>();
ArrayList<Defaultdisplay> defaultDisList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
SomeModel someModel = new SomeModel();
if (jsonObject.has("id")) {
someModel.setId(jsonObject.getString("id"));
}
if (jsonObject.has("name")) {
someModel.setName(jsonObject.getString("name"));
}
so on.....
if(someModel.getDisplay().equals("default")){
defaultDisList.add(someModel);
}else{
catDisList.add(someModel);
}
}

Json Parser returns size=0 Android

How can i look into this Json structure and store each element id name link picutre nb_album nb_fan radio,type into the list. Here is my json file.
{
"data": [
{
"id": "1214294",
"name": "The Pop Rock Boys",
"link": "http://www.deezer.com/artist/1214294",
"picture": "https://api.deezer.com/2.0/artist/1214294/image",
"nb_album": 7,
"nb_fan": 3,
"radio": false,
"type": "artist"
},
{
"id": "813196",
"name": "Ringtone Pop Rock",
"link": "http://www.deezer.com/artist/813196",
"picture": "https://api.deezer.com/2.0/artist/813196/image",
"nb_album": 0,
"nb_fan": 4,
"radio": false,
"type": "artist"
},
{
"id": "4165034",
"name": "Rock of Pop",
"link": "http://www.deezer.com/artist/4165034",
"picture": "https://api.deezer.com/2.0/artist/4165034/image",
"nb_album": 1,
"nb_fan": 0,
"radio": false,
"type": "artist"
},
{
"id": "4022223",
"name": "instrumental / Pop / Rock",
"link": "http://www.deezer.com/artist/4022223",
"picture": "https://api.deezer.com/2.0/artist/4022223/image",
"nb_album": 0,
"nb_fan": 1,
"radio": false,
"type": "artist"
}
],
"total": 4
}
You're parsing the initial string as an array. It isn't an array, its an object that contains a single array named "data". You need to parse it into a JSONObject, then get the array named data from it, then loop like you do.
try {
JSONArray articlesArray = new JSONObject(jString).getJSONArray("data");
JSONObject aObject;
for(int i=0; i<articlesArray.length(); i++){
aObject=articlesArray.getJSONObject(i);
SearchTrack a=new SearchTrack();
a.setId(aObject.getString("id"));
a.setLink(aObject.getString("link"));
a.setName(aObject.getString("name"));
a.setPicture(aObject.getString("picture"));
a.setNbAlbum(aObject.getString("nb_album"));
a.setNbFan(aObject.getString("nb_fan"));
a.setRadio(aObject.getString("radio"));
a.setType(aObject.getString("type"));
articles.add(a);
}
}catch (JSONException e) {
e.printStackTrace();
}

skipping null values when parsing json in android?

I am parsing json object from facebook.
In my facebook json object, there are string keys inside key array "data".
My Code as follows,
"data": [
{
"id": "100001211447563_300696056647440",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee is now using Facebook in English (US).",
"story_tags": {
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-19T09:13:04+0000",
"updated_time": "2012-01-19T09:13:04+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298802933503419",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee started using Graph API Explorer.",
"story_tags": {
"23": [
{
"id": 145634995501895,
"name": "Graph API Explorer",
"offset": 23,
"length": 18
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"picture": "http://photos-a.ak.fbcdn.net/photos-ak-snc1/v43/11/145634995501895/app_1_145634995501895_4870.gif",
"link": "http://developers.facebook.com/tools/explorer/",
"caption": "A tool to help you browse objects within the Facebook Graph API, manage permissions, obtain access tokens and generally learn how it all works.",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
},
{
"name": "Like",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
}
],
"type": "link",
"created_time": "2012-01-16T09:47:57+0000",
"updated_time": "2012-01-16T09:47:57+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298789650171414",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee likes myFBCovers.com.",
"story_tags": {
"15": [
{
"id": 160901873998019,
"name": "myFBCovers.com",
"offset": 15,
"length": 14
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-16T08:57:33+0000",
"updated_time": "2012-01-16T08:57:33+0000",
"comments": {
"count": 0
}
}
when I parsing this json object, String key "likes" is sometimes exist and sometimes do not exist in "data" jsonArray. So when I parsing JsonString "likes", sometimes I gets JSONException since I am parsing null value. Is there any way to skipping "null" json value?
Please help me regarding this.
if try catch it, next processes will be skipped.
you can check by isNull("name") method.
Edit: this is to check key. since you mentioned key exists sometimes. did you mean the value?
use JSONObjects method has(key)
data = (JSONObject) new JSONTokener(jsonObjRecv.toString()).nextValue();
if data.has("like"){
...
}
to check whether the value is null check with isNull("name")

Categories

Resources