I have a JSON format like this
{"response":{"status":true,"result":"user_exists"}}
Now i am trying to retrieve the Status value to do some logic
JSONObject jData = new JSONObject(data);
JSONArray response = jData.getJSONArray("response");
But i am getting the following error
org.json.JSONException: Value {"result":"user_exists","status":true}
at response of type org.json.JSONObject cannot be converted to
JSONArray
how to retrieve an Object from inside and Object ?
response is a JSONObject, not a JSONArray. Array objects are surrounded by these [] brackets, objects are with the normal ones {}. (See json.org for more format information)
Change
JSONArray response = jData.getJSONArray("response");
to
JSONObject response = jData.getJSONObject("response");
you are trying to retreive the status attribut from a JSONArray but , you don't have any JSONArray in your Code , ( JSONArray is surrounded by [] , and JSONObject is surrounded by {} ) ,
So to retreive the status value , try this :
JSONObject jData = new JSONObject(data);
JSONObject response = jData.getJSONObject("response");
boolean status = response.getBoolean("status");
response isn't an array but an object. Use getJSONObject and JSONObject instead of getJSONArray and JSONArray.
You have to first navigate to the response object by
JSONObject response = jData.getJSONObject("response") instead of JSONArray, as response is a object.
Related
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'm getting error cannot convert String to json object ..
While I'm converting this String
{"user_id": "user_id: 140" };
Error getting due to the format problem?
Thanks in advance..
try this JsonObject object=new JsonObject(jsonString); your string will be converted to JsonObject.
{"user_id": "user_id: 140" };
Please make sure that your json string is true.
Example JSON String:
[{"property":"value"}, {"property":"value"}, ...]
OR
{"property":[{"property":"value"}]}
OR
...
So may be your JSON String is not correct.
You can try with (Without ";" at the end of line)
{"user_id":"140"}
If you assign user_id: 140 as value then:
{"user_id":"user_id:140"}
Please make sure that at the end of line have no ;
And, how to parse JSON String???
The first {} => Object, [] => Array
Object without name
JSONObject jObject = new JSONObject(jsonString);
Object without name but in JSONArray
JSONObject jObject = jsonArray.getJSONObject(index); // example: index = 0
Object with name and in JSONArray
JSONObject jObject = jsonArray.getJSONObject("name_of_object");
JSONArray is like JSONObject & jsonArray above is an instance of JSONArray
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");
How can I parse json from the URL? below is my json structure which does not has tags.
[{"channelId":"0465CDBE","channelName":"ATV2"},{"channelId":"06E6923B1","channelName":"Phoenix"},{"channelId":"07B4FB7ed","channelName":"N24"},{"channelId":"115B73E39","channelName":"ORF2"},
Simply get the JSONArray
JSONArray jArr = new JSONArray(jsonString);
for(int i=0;i<jArr.length;i++)
{
String jChannel = jArr.getJSONObject(i).getString("channelId");
String jChannelName = jArr.getJSONObject(i).getString("channelName");
//you can now play with these variables or add to some list or do whatever you like.
}
The outer object is a JSONArray, so you can iterate it with a "for".
Inside your json array, you have simple json objects. You can parse it by key or iterate the keys.
You can user json object and get the value with getJSONArray
examples:
testob = {"channelId":"07B4FB7ed","channelName":"N24"}, {"channelId":"115B73E39","channelName":"ORF2"},
JSONObject jsonObject = new JSONObject(testob);
JSONArray dataArray = jsonObject.getJSONArray("data");
JSONObject jsonProductData = dataArray.getJSONObject(0);
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");