I am really new to JSON Parsing and learning everyday.
I have a specific JSON response I have to parse but I am finding no luck doing it.
I am using Volley for Parsing Request.
Here is my response:
{
"error": false,
"message": "Favourties fetched successfully",
"code": 200,
"data": [
{
"id": "5f1980f8c42e1f60854c57e4",
"type": 1,
"status": 1,
"favourite": {
"_id": "5f118057f44ebd1cead089db",
"firstName": "Bilal",
"lastName": "Khan",
"businessName": "Master Paint",
"image": "https://welist-assets.s3.us-west-2.amazonaws.com/profile_images/1592383845941-default_avatar.png"
},
"createdAt": "2020-07-23T12:22:16.731Z",
"updatedAt": "2020-07-23T12:22:16.731Z"
},
{
"id": "5f198084c42e1f60854c57e2",
"type": 3,
"status": 1,
"favourite": {
"images": [],
"_id": "5f12d5345478a53584eca98b",
"name": "Water Paint7"
},
"createdAt": "2020-07-23T12:20:20.680Z",
"updatedAt": "2020-07-23T12:20:20.680Z"
}
]
}
I am able to get the Array which contains 2 object, but I am unable to get the data inside the nested favorites.
try {
Log.d("Onresponse", response);
//converting the string to json array object
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("data");
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject favorites = array.getJSONObject(i);
Log.d("lenght", favorites.getString("status"));
//adding the product to product list
favoriteList.add(new FavoriteVendorsModel(
favorites.getString("firstName"),
favorites.getString("lastName"),
favorites.getString("businessName"),
favorites.getString("image")
));
}
Here is my method.
You have skipped one level of nesting. By calling array.getJSONObject(i) you get:
{
"id": "5f1980f8c42e1f60854c57e4",
"type": 1,
"status": 1,
"favourite": {
"_id": "5f118057f44ebd1cead089db",
"firstName": "Bilal",
"lastName": "Khan",
"businessName": "Master Paint",
"image": "https://welist-assets.s3.us-west-2.amazonaws.com/profile_images/1592383845941-default_avatar.png"
},
"createdAt": "2020-07-23T12:22:16.731Z",
"updatedAt": "2020-07-23T12:22:16.731Z"
}
And you are trying to call directly on that object next getString methods:
favorites.getString("firstName");
favorites.getString("lastName");
favorites.getString("businessName");
favorites.getString("image");
When actually you must first get JSONObject named favourite and then call these getString method on it:
JSONObject favorite = favorites.getJSONObject("favourite");
favorite.getString("firstName");
favorite.getString("lastName");
favorite.getString("businessName");
favorite.getString("image");
Related
for example:
{
"error": false,
"message": "",
"code": "",
"ver": "v1.0.0",
"base": "https://,,,,,,,,,.com/",
"count": 1,
"updated": "2020-10-28T19:40:51+03:00",
"data": [
{
"id": "44",
"slug": "human-resources",
"name": "Human resources",
"img1": null,
"img2": "page_44/1560347480_2.jpg",
"date": "2019-05-31",
"des": "'vvvvvv.v",
"sendMail": "info#....",
"iframe_url": "ff/ff"
}
]
}
this is api json response.
I want only "data": [] value, not the rest.
Is there a way I can avoid creating data class for whole api response instead just request "data": [..] value?
I am using Android Studio Kotlin Retrofit Moshi
If I understand your question correctly, you just want to extract data from that JSON string. You can do that with:
JSONArray dataArray;
try {
dataArray = new JSONObject(yourString).optJSONArray("data");
} catch (JSONException e) {
// whatever
}
for (int i = 0; i < dataArray.length(); ++i) {
// do whatever you want with each object:
String name = dataArray.optJSONObject(i).optString("name");
}
I am trying Expandable Recycler view using Volley and thoughtbot expandablerecyclerview. Please see my JSON Array below.
{
"Ravi Shankar": [{
"staffname": "Ravi Shankar",
"taskname": "tesing1 task",
"id": "1"
}, {
"staffname": "Ravi Shankar",
"taskname": "testing 2 task",
"id": "2"
}, {
"staffname": "Ravi Shankar",
"taskname": "IMAGE TESTING",
"id": "4"
}, {
"staffname": "Ravi Shankar",
"taskname": "IMAGE NEW",
"id": "5"
}],
"Ritesh": [{
"staffname": "Ritesh",
"taskname": "testing3 task",
"id": "3"
}]
}
This array was group by employee name. I need to get the group name as a string and child array in a loop.
I tried below code but it's not given as I expected.
for(int i=0;i<response.length();i++){
JSONObject jsonObject = response.getJSONObject(i);
String tstaffname = jsonObject.has("staffname")?jsonObject.getString("staffname"):"";
String ttaskname = jsonObject.has("taskname")?jsonObject.getString("taskname"):"";
String tid = jsonObject.has("id")?jsonObject.getString("id"):"";
//addTasks(tstaffname,ttaskname);
Log.e("EmpName", ttaskname);
tasksNames.add(new TasksName(ttaskname));
StaffName staffName = new StaffName(tstaffname, tasksNames);
staffNames.add(staffName);
}
Please comment for any clarification.
My response have many values, but one is filter object that are coming some time in array nad some time in object.
How to find this one is object or array, i am using Gson in android.
Comming in array like this,
"filter": [
{
"id": 3,
"level": "0",
"parent_id": 0,
"category_name": "Women",
"LEVEL1": [
{
"id": 130,
"level": "1",
"parent_id": 3,
"parent_name": "Women",
"category_name": "Saree",
"LEVEL2": [
{
"id": 152,
"level": "2",
"parent_id": 130,
"parent_name": "Saree",
"parent_parent_id": 3,
"parent_parent_name": "Women",
"category_name": "Party Wear"
}
]
}
]
Some time coming like this,
"filter": {
"0": {
"attribute_title": "Brand",
"attribute_id": 1,
"childs": [
{
"child_attribute_name": "Ambica",
"child_attribute_id": 530,
"count": 38
}
]
}
I am using Gson then how to identify in code,
Using GSON API :
Create JsonObject from response json and pass it to following method :
public Class<?> getJSONType(JsonObject jsObj) {
GsonBuilder gBuilder = new GsonBuilder();
Gson gson = gBuilder.create();
Object obj = gson.fromJson(jsObj.get("filter"), Object.class);
if(obj instanceof List){
return List.class;
}else if(obj instanceof Map){
return Map.class;
}
return null;
}
Here I have considered that filter is top level element in your JSON.
The code jsObj.get("filter") should be modified to reach up to correct location.
Can you help me with this
how can i parse this Json Array. i'm getting error type mismatch
Thanks in Advance.
{"response": [
{
"feed": [
{
"rating": 1,
"lname": "",
"surname": "xxxxx",
"date": "2015-04-05 102:51:35",
"details": "check"
},
{
"rating": 2,
"name": "",
"avatar": ""
}
]
}]}
Use
JsonObject jsonObject = new JsonObject(<your response>);
incase you are getting your response as a json object you can ignore the above.
next
jsonArray responseArray= jsonObject.getJsonArray("response");
jsonObject feedObject = responseArray[0];
jsonArray feedArray = feedObject.getJsonArray("feed");
Now that you have your feedArray you can traverse it to get all its data.
I am trying to parse data from a MongoDB cloud server. My JSON data returned from the server is as follows:
[
{
"_id": {
"$oid": "4e78eb48737d445c00c8826b"
},
"message": "cmon",
"type": 1,
"loc": {
"longitude": -75.65530921666667,
"latitude": 41.407904566666666
},
"title": "test"
},
{
"_id": {
"$oid": "4e7923cb737d445c00c88289"
},
"message": "yo",
"type": 4,
"loc": {
"longitude": -75.65541383333333,
"latitude": 41.407908883333334
},
"title": "wtf"
},
{
"_id": {
"$oid": "4e79474f737d445c00c882b2"
},
"message": "hxnxjx",
"type": 4,
"loc": {
"longitude": -75.65555572509766,
"latitude": 41.41263961791992
},
"title": "test cell"
}
]
The problem I am having is the data structure returned does not include a name of the array of JSON objects. Each object returned is a "post". But how do I parse it using GSON if the there is no name for the array of JSON objects. I would like to put these "posts" into an ArrayList of type Post.
Piece of code you are looking for:
String jsonResponse = "bla bla bla";
Type listType = new TypeToken<List<Post>>(){}.getType();
List<Post> posts = (List<Post>) gson.fromJson(jsonResponse, listType);
Use the contructor for JSONArray to parse the string:
//optionally use the com.google.gson.Gson package
Gson gson = new Gson();
ArrayList<Post> yourList = new ArrayList<Post>();
String jsonString = "your string";
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++){
Post post = new Post();
//manually parse for all 5 fields, here's an example for message
post.setMessage(jsonArray.get(i).getString("message"));
//OR using gson...something like this should work
//post = gson.fromJson(jsonArray.get(i),Post.class);
yourList.Add(post);
}
Considering there are only 5 fields, using Gson might be more overhead than you need.