Not able to load nested Json Array - android

My Json Value is
{"candidateName":"Check","success":true,"candidateWorkExperience":"[{\"candidateWorkExperienceId\":7486,\"candidateBiographyRef\":{\"primaryKey\":98567,\"percent\":0.0,\"specialization\":0,\"rating\":0,\"description\":\"Check\",\"properties\":{},\"secondaryKey\":0,\"start\":0,\"limit\":0,\"noOfStud\":0,\"typingTest\":false,\"isPostGrad\":false},\"designation\":\"Check\",\"company\":\"Check\",\"startDate\":\"Jan 1, 2012\",\"endDate\":\"Jan 1, 2014\",\"workTypeMasterRef\":{\"primaryKey\":2,\"percent\":0.0,\"specialization\":0,\"rating\":0,\"description\":\"Permanent\",\"properties\":{},\"secondaryKey\":0,\"start\":0,\"limit\":0,\"noOfStud\":0,\"typingTest\":false,\"isPostGrad\":false},\"achivements\":\"Check\",\"candidateRecordStatus\":\"EXISTING\",\"currentCompany\":\"false\"}]"}
I am trying to pass the same by
JSONArray jarray = jsono.getJSONArray("candidateWorkExperience");
But I am getting org.json.JSON.typeMismatch exception.
Can any one suggest how to load the json data into the array.

Your problem is that you put "[]" which js is interpreting as a string not an array.
The arrays are enclosed by [] with no ".
Try the next json and let me know if this fix your problem:
{
"candidateName": "Check",
"success": true,
"candidateWorkExperience": [
{
"candidateWorkExperienceId": 7486,
"candidateBiographyRef": {
"primaryKey": 98567,
"percent": 0.0,
"specialization": 0,
"rating": 0,
"description": "Check",
"properties": {
},
"secondaryKey": 0,
"start": 0,
"limit": 0,
"noOfStud": 0,
"typingTest": false,
"isPostGrad": false
},
"designation": "Check",
"company": "Check",
"startDate": "Jan1, 2012",
"endDate": "Jan1, 2014",
"workTypeMasterRef": {
"primaryKey": 2,
"percent": 0.0,
"specialization": 0,
"rating": 0,
"description": "Permanent",
"properties": {
},
"secondaryKey": 0,
"start": 0,
"limit": 0,
"noOfStud": 0,
"typingTest": false,
"isPostGrad": false
},
"achivements": "Check",
"candidateRecordStatus": "EXISTING",
"currentCompany": "false"
}
]
}

Your JSON is not correct, your "candidateWorkExperience" parameter is not a JSONArray but a JSONString.
Paste your JSON here
And you will see how you JSON is parsed.
Cheers !

Related

Moshi parse two-dimensional array

How to use Moshi+Retrofit to parse the data format of the two-dimensional array type returned by the server
{
"code": 200,
"msg": null,
"data": {
"page": 1,
"size": 10,
"total": 2,
"inner_data": [
[{
"transaction_id": "1626105263092142080",
"creat_time": "1676528680953"
}]
]
}
}

How to write json Objects in order wise?Android

I am writing a json file to my external storage. Below code is used to create json objects and then adding them to json array
JSONObject jsonObj = new JSONObject();
jsonObj.put("slotid", "1a");
jsonObj.put("updatetime", 1477650798);
jsonObj.put("active", true);
jsonObj.put("adurl", "httpAd");
jsonObj.put("imgurl", "httpImg");
jsonObj.put("hd", false);
jsonObj.put("x", 0);
jsonObj.put("y", 0);
phoneArray.put(jsonObj);
jsobObj.put("slots", phoneArray);
Above code works fine for API24 but when i try to run same code for API 19 then following output occurs
{
"containers": [ ],
"slots": [
{
"slotid": "1a",
"updatetime": 1477650798,
"adurl": "httpAd",
"active": true,
"hd": false,
"y": 0,
"x": 0,
"imgurl": "httpImg"
},
{
"slotid": "1a",
"updatetime": 1477650798,
"adurl": "httpAd",
"active": true,
"hd": false,
"y": 0,
"x": 0,
"imgurl": "httpImg"
}
]
}
My question is why my jsonObjects are not writing in order? I am successfully getting correct output on API 24 i.e
{
"slots": [
{
"slotid": "1a",
"updatetime": 1477650798,
"active": true,
"adurl": "httpAd",
"imgurl": "httpImg",
"hd": "false",
"x": 0,
"y": 0
},
{
"slotid": "1a",
"updatetime": 1477650798,
"active": true,
"adurl": "httpAd",
"imgurl": "httpImg",
"hd": "false",
"x": 0,
"y": 0
}
],
"containers": []
}
Where should be the problem?

Suggest a Future builder method for the following json

{
"totalHits": 500,
"hits": [
{
"largeImageURL": "https://pixabay.com/get/52e3d6414352af14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 424,
"webformatWidth": 640,
"likes": 11,
"imageWidth": 4456,
"id": 4332903,
"user_id": 1151065,
"views": 697,
"comments": 14,
"pageURL": "https://pixabay.com/photos/evangelion-mech-unit-02-robot-4332903/",
"imageHeight": 2958,
"webformatURL": "https://pixabay.com/get/52e3d6414352af14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 99,
"tags": "evangelion mech unit 02, robot, fighter",
"downloads": 311,
"user": "vinsky2002",
"favorites": 0,
"imageSize": 878944,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/05/15/02-40-06-911_250x250.png",
"previewURL": "https://cdn.pixabay.com/photo/2019/07/12/13/44/evangelion-mech-unit-02-4332903_150.jpg"
},
{
"largeImageURL": "https://pixabay.com/get/55e7d54b4d51a514f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 359,
"webformatWidth": 640,
"likes": 18,
"imageWidth": 3863,
"id": 3708739,
"user_id": 10248012,
"views": 2696,
"comments": 2,
"pageURL": "https://pixabay.com/photos/sky-dark-night-moon-moonlight-3708739/",
"imageHeight": 2170,
"webformatURL": "https://pixabay.com/get/55e7d54b4d51a514f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 84,
"tags": "sky, dark, night",
"downloads": 1036,
"user": "Sritam_kumar_Sethy",
"favorites": 25,
"imageSize": 2027189,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/09/28/19-27-53-806_250x250.jpeg",
"previewURL": "https://cdn.pixabay.com/photo/2018/09/28/08/37/sky-3708739_150.jpg"
},
{
"largeImageURL": "https://pixabay.com/get/57e2dc4a4c5aaf14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_1280.jpg",
"webformatHeight": 360,
"webformatWidth": 640,
"likes": 14,
"imageWidth": 4608,
"id": 1299683,
"user_id": 1151065,
"views": 2932,
"comments": 3,
"pageURL": "https://pixabay.com/photos/panda-z-panda-toy-children-child-1299683/",
"imageHeight": 2592,
"webformatURL": "https://pixabay.com/get/57e2dc4a4c5aaf14f6da8c7dda79367b153fdee254506c4870277bd3934ec45ebf_640.jpg",
"type": "photo",
"previewHeight": 84,
"tags": "panda-z, panda, toy",
"downloads": 1067,
"user": "vinsky2002",
"favorites": 7,
"imageSize": 1745060,
"previewWidth": 150,
"userImageURL": "https://cdn.pixabay.com/user/2019/05/15/02-40-06-911_250x250.png",
"previewURL": "https://cdn.pixabay.com/photo/2016/04/01/09/56/panda-z-1299683_150.jpg"
},
],
"total": 588
}
The API I'm fetching returns json value like this. I wrote the following code to extract the data, it works if I code it like this and the widget builds, but with this error before the widget loads
Future getJsonData() async{
var response;
for(int i=1; i<=5; i++){
response = await http.get(
Uri.encodeFull(widget.api+i.toString()),
headers: {"Accept": "application/json"}
);
setState(() {
var toJsonData = json.decode(response.body);
item.addAll(toJsonData['hits']);
});
}
print(response.body);
return item;
}
error image
But when I use FutureBuilder method, it fails to create the widget. It would be helpful if someone can tell a possibly working FutureBuilder method for this json data.

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

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.

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();
}

Categories

Resources