hi i have only one parameter to parse
this is what i have tried....
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
and here is the below json
{"result" : "169489465811879423"}
Use this
JSONObject jsonObject = new JSONObject(response);
String result = jsonObject.getString("result");
Here there is no JsonArray and you are trying to get an array
Related
Currently I have this JSON structure:
{"title1":["info1","info2"],"title2":"info2","title3":"info3"}
What I need to achieve is this:
{"title4":[{"a":"aa","b": "bb"}]}
To achieve the above, I use JSONArray:
JSONArray list = new JSONArray();
list.put("info1");
list.put("info2");
obj.put("title1", list);
And JSONObject:
JSONObject obj = new JSONObject();
obj.put("title2", "info2");
How can I visualize the desired output?
To achieve what you want, a JSONObject with a JSONArray containing JSONObjects, you can use the following code
JSONObject root = new JSONObject(); // The root JSON object
JSONArray title4 = new JSONArray(); // The JSON array that will contain JSON objects
// The JSON objects
JSONObject a = new JSONObject();
a.put("a", "aa");
JSONObject b = new JSONObject();
b.put("b", "bb");
title4.put(a);
title4.put(b);
// Put the JSON array in the root JSON object
root.put("title4", title4);
My below android code is throwing
org.json.JSONException: Value
{"ID":1,"DisplayName":"Manish","UserName":"manish.parab#hotmail.com"}
at AuthenticateUserResult of type org.json.JSONObject cannot be
converted to JSONArray
Code:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = jsonObject.getJSONArray("AuthenticateUserResult");
response is string from WCF method.
{"AuthenticateUserResult":{"DisplayName":"Manish","ID":1,"UserName":"manish.parab#hotmail.com"}}
The value of AuthenticateUserResult is a JSON Object (it's enclosed in {}).
Change that line to this
JSONObject jArray = jsonObject.getJSONObject("AuthenticateUserResult");
Then you can get your data as follows :
String displayName = jArray.getString("DisplayName");
// Etc...
There are three workarounds to solve this problem.
1.Use JsonObject. Your WCF server just give it in JsonObject.
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response).getJSONObject("AuthenticateUserResult");
2.Use json array as a container
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONArray jArray = new JSONArray().put(jsonObject.getJSONObject("AuthenticateUserResult"));
3.Edit server to provide AuthenticationUserResult into json array. The right format would be as below.
{"AuthenticateUserResult":[{"DisplayName":"Manish","ID":1,"UserName":"manish.parab#hotmail.com"}]}
The exception is right, because "AuthenticateUserResult" value is declared as an element ({}) and not as an array ({}).
To fix this, use getJSONObject method to get the value of "AuthenticateUserResult", like this:
String response = Common.ExecuteHttpRequest(Url);
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("AuthenticateUserResult");
After that, you can retrieve a child element, like:
String mUserName = result.getString("UserName");
I have JSONObject and I want get 8 object A,B,C...
my json string:
{"Matches":{"A":[{"team1":"Russia","team2":"France","time":"00:00:00","time0_90":"20","score":"0 : 0","stadium":"Maracana","referee":"ref","group":"A"},{"team1":"Portugal","team2":"Honduras","time":"00:00:00","time0_90":"60","score":"0 : 2","stadium":"","referee":"","group":"A"}]},"success":1}{"Matches":{"B":[{"team1":"Brazil","team2":"Spain","time":"00:00:00","time0_90":"3","score":"1 : 0","stadium":"","referee":"","group":"B"}]},"success":1}{"Matches":{"C":[]},"success":0}{"Matches":{"D":[]},"success":0}{"Matches":{"E":[]},"success":0}{"Matches":{"F":[]},"success":0}{"Matches":{"G":[]},"success":0}{"Matches":{"H":[]},"success":0}
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMathes = jsonResponse.optJSONArray("matches");
// ????????
Your response structure is:
JSONObject
key:value
JSONObject (key: "Matches")
JSONArray (key: "A")
JSONObject,
key:value,
key:value,
etc...
JSONObject
To get access to A, follow the steps below:
Create JSONObject from your response:
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
Get JSONObject for the key "Matches"
JSONObject jsonMatches = jsonResponseObj.getJSONObject("Matches");
This object contains a JSONArray for the key "A", so let's get that array:
JSONArray jsonArrayA = jsonMatches.optJSONArray("A");
For your response, you have 2 JSONObjects in this array, so first, let's declare and initialize them:
//two JSONObjects
JSONObject[] jsonObjects = new JSONObject[jsonArrayA.length()];
//go through the array of JSONObjects and fetch them
for (int i=0; i < jsonObjects.length; i++) {
jsonObjects[i] = jsonArrayA.getJSONObject(i);
}
You now have A as JSONArray in jsonArrayA
A contains 2 JSONObjects, and you have them in jsonObjects[0] and josnObjects[1]
If you want to get the contents of those jsonObjects, simply fetch it using the keys, eg.:
String team1Obj1 = jsonObjects[0].getString("team1"); // will contain 'Russia'
String team2Obj2 = jsonObjects[1].getString("team2"); // will contain 'Honduras'
String stadium1 = jsonObjects[0].getString("stadium"); // will contain 'Maracana'
etc.
When I am trying to access the Content of this below JSON , I am getting the Exception.
This is My JSON Value:
{"listDev":{"description":"D4684","deviceID":"d2","uniqueID":"0014018682"}}
This is my code to get the Content in "listDev"
JSONObject object = ApplicationContext.getHttpService().readAsJson(content);
JSONArray array = object.getJSONArray("listDev");
So, what to do to get the Contents like decription, imei...
Thanks in advance.
listDev is a JSONObject not a JSONArray:
change
JSONArray array = object.getJSONArray("listDev");
to
JSONObject array = object.getJSONObject("listDev");
String json = {"listDev":{"description":"D4684","deviceID":"d2","uniqueID":"0014018682"}};
JSONObject object = new JSONObject(json);
JSONObject array = object.getJSONArray("listDev");
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");