how to parse JSON string with value as array - android

i need to parse the JSON data given below.
{"result":[{"bookId":142645,"bookpb":"MF",
"bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1},
"hierarchies":{"categories":["4"],"events":[]},"shopId":769752},
upto "sold" it is working fine.but when i am trying to parse categories it is not working.
given below is the code for parsing the data.
ArrayList<BookItem> resultdata = new ArrayList<BookItem>();
JSONArray jsonArray = (new JSONObject(inputString))
.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
item = new BookItem();
item.setbookId(jsonObject.optString(book_ID));
item.setPurchase(jsonObject.optInt(PURCHASE));
item.setArrival(jsonObject.optInt(ARRIVAL));
item.setSold(jsonObject.optInt(SOLD));
item.setbookTs(jsonObject.optString(book_TS));
JSONObject hierarchies=jsonObject.getJSONObject(HIERARCHY);
item.setCategory(hierarchies.getInt("categories"));
resultdata.add(item);
}
can anybody help me???
i came to know that this is the problem of
{"categories":["4"],"events":[]}
data.how can i parse this array value?

categories is an JSONArray in order to get JSONArray
replace
item.setCategory(hierarchies.getInt("categories"));
with
item.setCategory(hierarchies.getJSONArray("categories").getInt(0));

For your purpose the best solution is probably GSON library. It do serialization and deserialization on its own and you will get your objects.
http://code.google.com/p/google-gson/

Related

parsing nested JSON array using volley library in android

How can I parse nested JSON array using volley library?
My JSON data structure screenshot.
https://prnt.sc/pbaea5
I need to perse score value.
JSONArray jsonArray = response.getJSONArray("matches");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjectMatchs = jsonArray.getJSONObject(i);
// bat_team node is JSON Object
JSONObject bat_teamData = jsonObjectMatchs.getJSONObject("bat_team");
JSONArray jsonArrayInnings = bat_teamData.getJSONArray("innings");
JSONObject jsonObjectInnings = jsonArrayInnings.getJSONObject(i);
String bat_team_score = jsonObjectInnings.getString("score");
}
You need to iterate further on
jsonArrayInnings using another loop but in your code you are using i of parent loop.
It will not work properly.

How to parse Multiple json array in android?

This may be possible of duplicate question but am struggling with this am getting json array response like this:
[{"data":"25"},{"MobID":"88"}]
JsonArray jsonarray=new JsonArray(serverresponse);
for(int i=0;i<jsonarray.length();i++){
JsonObject json=new JsonObject(i);
String data=json.getInt("data");
String Mobid=jsong.getInt("MobID");
}
}
Is it possible to parse this type of json i haven't found any parsing method for this above method as a beginner am struggling with this you people are here to help beginner like this Thanks in advance!!!
Try this:
JSONArray array = new JSONArray(serverResponse);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
int data = object.getInt("data");
int mobid = object.getInt("MobID");
// use them ...
}
Hint: the secret (sh!) is to read the javadocs! All of the methods you needed are in the JSONArray and JSONObject javadocs.
Note: I corrected a number of style errors in your code (and some bugs). Please compare your version and mine to see what I fixed.

Parse JSON string from remote URL in Android

Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}

Trouble on creating a JSON Object on Android

I'm having a provblem on creating a JSON object on android app.
I's getting a JSON array like :
[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]
from the server (using a servlet).
But when i try to access the array objects on a for cicle like:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
}
my app gives some error, and simple stop (something like an ANR! i'm not sure about this).
Can anyone help? please.
First, please remove the exclamation mark "!" at the end of your sentences, it might offend somebody here ^^
Okay, onto your problem, I suggest to encapsulate your JSON array inside a json object, maybe like this :
{"data":[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]}
For example, we save that result string inside a String called jsontext, then we would create a JSONObject from that string, grab the JSONArray, and parse the JSONObject on the array. Here's a little snippet :
String jsontext = "yourjsontext";
//if it's from a http response, you might call respon
JSONObject mainObject = new JSONObject("jsontext");
JSONArray jsonArray = mainObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
} code here
Hope this helps, good luck ^^
Regards,
Reid

Simple Json parsing in android

I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");

Categories

Resources