Parsing json (2 arrays) in Android - android

I need to parse a JSON with two arrays.
resp: {"attending": [], "people": []}
I tried this
JSONObject AttendingArray = new JSONObject(resp);
JSONArray ParkArray = new JSONArray("people");
JSONArray AttendingArray = new JSONArray("attending");
But it doesn't work
12-01 22:47:53.074: WARN/System.err(30814): org.json.JSONException: Value people of type java.lang.String cannot be converted to JSONArray
Thanks!

JSONObject obj = new JSONObject(resp);
JSONArray ParkArray = new JSONArray(obj.getString("people"));
JSONArray AttendingArray = new JSONArray(obj.getString("attending"));
You must pass actual JSON to the JSONArray constructor, not just the name of the json array - the new JSONArray constructor call doesn't know about the response, so you need to give it some data, not just "people"

Related

JsonArray converting Array into StringArray

I created the following String
[{"lat":"24.8809436","lng":"67.0638776","timeStamp":1527155905396}]
Now i am converting this String to a JsonArray.
jsonArray = new JsonArray();
jsonArray.add(stringArray)
After this line it converts the above mentioned String Array into this format.
["[{\"lat\":\"24.8809436\",\"lng\":\"67.0638776\",\"timeStamp\":1527155905396}]"]
This was working fine when i was using JSONArray, but since now i am now using retrofit, I switched over to JsonObject and JsonArray.
EDIT
stringArray ="[{"lat":"24.8809436","lng":"67.0638776","timeStamp":1527155905396}]";
jsonArray = new JsonArray();
jsonArray.add(stringArray);
JsonObject jsonobject= new JsonObject();
jsonobject.add("trail", jsonArray);
don't put manually use this code as reference.
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("lat", "24.8809436");
jsonObject.put("lng", "67.0638776");
jsonObject.put("timeStamp", "1527155905396");
jsonArray.put(jsonObject);
this is quite simple and standard code format.
If I get your question, you want to convert the string:
[{"lat":"24.8809436","lng":"67.0638776","timeStamp":1527155905396}]
into a JSON Array.
You should call:
jsonArray = new JSONArray(stringArray);
This will create a new JSON array from the string if it is correctly formatted JSON to begin with.

JSON object building

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);

(Android) JSONObject cannot be converted to JSONArray

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");

Android parse JSON, how to fetch only single parameter

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

"org.json.JSONObject" cannot be converted to 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");

Categories

Resources