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