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.
Related
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");
I'm working on an android project where I have to show some items in response of a request I'll send to server using provided API.
I'll get an JSON object in response which contains a JSON array as one of the elements inside of it.
My problem is the JSON array may have several JSON objects inside of it and how to handle this situation is beyond my knowledge.
"Details": [
{
"OrderId": 7615,
"ProductId": 1292406,
"ProductImageFormat": "jpg",
"ProductImageId": 1752,
"ProductImageUrl": "",
"ProductOrgImageUrl": "",
"ProductLargeImageUrl": "",
"ProductName":""
"ProductSlug": ""
"ProductSubTitle": ""
"Quantity": 1,
"UnitPrice": 8300
},
{
"OrderId": 7615,
"ProductId": 23568452,
"ProductImageFormat": "jpg",
"ProductImageId": 1895,
"ProductImageUrl": "",
"ProductOrgImageUrl": "",
"ProductLargeImageUrl": "",
"ProductName":""
"ProductSlug": ""
"ProductSubTitle": ""
"Quantity": 1,
"UnitPrice": 3500
}
]
I may get several of this single object with different values in one Array as you can see above.
Is there anything I can do about this?
EDIT: what I mean is that the array size is dynamic and I don't know how to handle dynamic sized JSON array
first of all, the JSON you provided is invalid. So I'm going to correct it and assume the following (the "Details" array is wrapped in a JSONObject)
{
"Details": [
{
"OrderId": 7615,
"ProductId": 1292406,
"ProductImageFormat": "jpg",
"ProductImageId": 1752,
"ProductImageUrl": "",
"ProductOrgImageUrl": "",
"ProductLargeImageUrl": "",
"ProductName":"",
"ProductSlug": "",
"ProductSubTitle": "",
"Quantity": 1,
"UnitPrice": 8300
},
{
"OrderId": 7615,
"ProductId": 23568452,
"ProductImageFormat": "jpg",
"ProductImageId": 1895,
"ProductImageUrl": "",
"ProductOrgImageUrl": "",
"ProductLargeImageUrl": "",
"ProductName":"",
"ProductSlug": "",
"ProductSubTitle": "",
"Quantity": 1,
"UnitPrice": 3500
}
]
}
Get the array and iterate it (assuming the outer object is called jo):
try {
JSONArray ja = jo.getJSONArray("Details");
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
Log.i("JSON", jsonObject.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
This way you can handle JSON arrays of arbitrary length.
I have a nested JSON array which I want to decode. I have already found a way to decode a specified info in the object but it's not working so good.
This is the code I used to decode (name, cover and street) from the JSON object:
JSONArray jsonArray = response.getJSONArray("data");
for (int i=0; i < jsonArray.length();i++){
JSONObject events = jsonArray.getJSONObject(i);
String name = events.getString("name");
JSONObject cover = events.getJSONObject("cover");
String imgurl = cover.getString("source");
JSONObject place = events.getJSONObject("place");
JSONObject loc = place.getJSONObject("location");
String street = loc.getString("street");
ItemListView item = new ItemListView(name,street,imgurl);
listItems.add(item);
adapter = new ItemListAdapter(listItems, getActivity());
recyclerView.setAdapter(adapter);
}
I want to decode more info like description, end_time and start_time.
The JSON data structure looks like this:
{
"data": [
{
"description": "",
"end_time": "",
"name": "",
"place": {
"name": "",
"location": {
"city": "",
"country": "",
"latitude": 0000000,
"longitude": 000000,
"street": ""
},
"id": ""
},
"start_time": "",
"id": ""
},
{
"description": "",
"end_time": "",
"name": "",
"place": {
"name": "",
"location": {
"city": "",
"country": "",
"latitude": 0000000,
"longitude": 000000,
"street": ""
},
"id": ""
},
"start_time": "",
"id": ""
}
]
}
I put the decoded info in a ListView in my Android app and that is why I have an adapter and ListView in the code.
Any help is greatly appreciated :)
Thanks in advance.
This is simple you can get other string like this -:
JSONArray jsonArray = response.getJSONArray("data");
for (int i=0; i < jsonArray.length();i++){
JSONObject events = jsonArray.getJSONObject(i);
String description= events.getString("description");
String end_time= events.getString("end_time");
String start_time= events.getString("start_time");
}
Below is my code which parse JSON if I fetch "id". If I don't give id then the JSON array will not show. How do i show JSON without id?
How do I parse JSONArray without "id" if I don't give id in JSON file then array will not show it is necessary to give "id" in JSON file???
this json array not show
{
"status": 1,
"data": [
{
"title": "Elementary"
},
{
"title": "Middle"
},
{
"title": "High"
},
{
"title": "Atypical"
}
]
}
this json array will show
{
"status": 1,
"data": [
{
"id": "1",
"title": "Elementary"
},
{
"id": "2",
"title": "Middle"
},
{
"id": "3",
"title": "High"
},
{
"id": "4",
"title": "Atypical"
}
]
}
Code:
JSONObject json2 = new JSONObject(str);
status = json2.getString("status");
if (status.equals("1")) {
JSONArray school = json2.getJSONArray("data");
for (int i = 0; i < school.length(); i++) {
JSONObject object = school.getJSONObject(i);
// Category_ID.add(Long.parseLong(object.getString("id")));
Category_name.add(object.getString("title"));
}
You are accessing the id property here:
Category_ID.add(Long.parseLong(object.getString("id")));
When it is not there this will throw an exception.
Try This Code while parsing your json:
if(object.has("id"))
{
Category_ID.add(Long.parseLong(object.getString("id")));
}
else
{
// add default id
}
hope this will work for you.
I suppose you want your code to work for both JSON samples.
You can perform a check if "id" exists or not and then parse accordingly.
You can perform this check using the below code.
String id = object.optString("id");
//JSONObject.optString("id") will return id's value if it exists or else will return empty string.
if(!"".equals(id)){
Category_ID.add(Long.parseLong(id));
//this code will run only when your JSON contains id
}
else
{
//this code will run when your JSON doesn't have id field.
Category_ID.add();
}
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.