I have an JSON something like:
{
"store":"usa",
"values":["1","2","3","4"];
}
and so I go ahead and get a JSON object for values:
JSONObject values = json.getJSONObject("values");
So now I have this Json object but methods like getName("X") does not work for this type of JSON. There is no key value for the Array now. Its just Strings oen after another.
I want it to return like
String[] listValues = value.getArray();
But I don't see anything like this.
Any ideas ?
Thanks !!
Have you tried this?
JSONArray a = json.getJSONArray("values");
for (int i = 0; i < a.size(); i++) {
Log.d("Type", a.getString(i););
}
String myJson = "{
"store":"usa",
"values":["1","2","3","4"]
}";
JSONObject jsonObject = new JSONObject(myJson);
JSONArray array = jsonObject.getJSONArray("values");
Related
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");
}
I have a JSON Array as follows:
jSONArray: {"li":[["apple\r\n","orange\r\n","mango\r\n"]]}
I want only the list as [apple, orange, mango] in android. Can anybody explain how to parse it in android without \r\n. I have tried as follows:
JSONArray jsonArray1 = new JSONArray(results.toString()); // which is the JSON Array
for(int i=0; i < jsonArray1.length(); i++) {
JSONObject jsonobject = new JSONObject();
JSONObject issue = jsonArray1.getJSONObject(i);
String _pubKey = issue.getString("li");
}
So, I am getting pubKey as [["apple\r\n","orange\r\n","mango\r\n"]]. I dont want \r\n.
Just replace those characters while parsing.
Example: If fruit is the string you want to put in your JSONArray jsonArray, call:
jsonArray.put(fruit.trim().replace("\r", "").replace("\n", ""));
I'm trying to parse the following JSON string
String _message = "GetXTRONResult: \"[{\"xtron\":\"Acub1\"},{\"xtron\":\"Acub2\"},{\"xtron\":\"Acub3A\"}]\"";
JSONObject jsonObj = new JSONObject(_message);
//Try to convert to array
JSONArray array = jsonObj.getJSONArray("GetXTRONResult"); //FAILS !
What is the best way to parse the above please?
UPDATE:
This is what the value is during debugging:
{"GetXTRONResult":"[{\"xtron\":\"Acub1\"},{\"xtron\":\"Acub2\"},{\"xtron\":\"Acub3A\"}]"}
org.json.JSONException: Value .... at GetXTRONResultof type java.lang.String cannot be converted to JSONArray
SOLUTION THAT WORKED FOR ME:
I had to use the iterator as follows:
ArrayList list = new ArrayList();
JSONObject jsonObj = new JSONObject(_message);
Iterator<?> keys = jsonObj.keys();
if (keys.hasNext()) {
JSONArray array = new JSONArray((String) jsonObj.get((String) keys.next()));
for (int i = 0; i < array.length(); ++i) {
list.add(array.getJSONObject(i).getString("xtron").toString());
}
You have some errors in your json string, like "[.
You can't use quotes that wrap your list.
This one should work:
String _message = "{\"GetXTRONResult\": [{\"xtron\":\"Acub1\"},{\"xtron\":\"Acub2\"},{\"xtron\":\"Acub3A\"}]}";
JSONObject jsonObj = new JSONObject(_message);
//Try to convert to array
JSONArray array = jsonObj.getJSONArray("GetXTRONResult");
System.out.println(array);
Output: [{"xtron":"Acub1"},{"xtron":"Acub2"},{"xtron":"Acub3A"}]
To avoid your confusion when making JSON in java/android
you could use single quote (') instead of double quote (") for JSON inside Java Code
For example:
from
"{\"xtron\":\"Acub1\"},{\"xtron\":\"Acub2\"},{\"xtron\":\"Acub3A\"}"
to be something like this
"{'xtron':'Acub1'},{'xtron':'Acub2'},{'xtron':'Acub3A'}"
I think this is what you're looking for:
JSONObject rootObj = new JSONObject(jsonString);
String theArrayJSON = rootObj.getJSONArray("GetXTRONResult");
JSONObject theArray = new JSONObject(theArrayJSON);
I have the json like this..
{"source":[{"id":"22","name":"xyz"},{"id":"23","name":"ghj"},
{"id":"24","name":"tuv"}]}
I've to get the value of "id" and "name". How to do that..
I've tried a lot. But can't get the best solution how to do this.
thanks for the help
You want to use following code....
JSONObject jObject = new JSONObject(responseBody);
sourceArray = jObject.getJSONArray("source");
for (int i = 0; i < 3; i++) {
name[i] = sourceArray.getJSONObject(i).getString("name").toString();
id[i] = sourceArray.getJSONObject(i).getInt("id");
}
responseBody is your actual JSON String.
Reply if you have any doubts.
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");