how do I traverse an android JSONArray witin a JSONObject - android

I have a JSONObject with the data structure as follows
[{"distance":"200 meters","location_id":"519"},{"distance":"300 meters","location_id":"219"}]
and I'm trying to traverse each array within that object, I have the following code where locationArray is the valid JSONObject
for (int j = 0; j < locationArray.length(); j++) {
JSONObject j_obj;
j_obj = locationArray.getJSONArray(j); //error here
location_id = j_obj.getString("location_id");
}
but i'm getting an error trying to find each sub array of locationArray with an integer.

the solution:
JSONArray rootArray = new JSONArray(jsonString);
int len = rootArray.length();
for(int i = 0; i < len; ++i) {
JSONObject obj = rootArray.getJSONObject(i);
location_id = obj.getString("location_id");
}
There is an error in your code. j_obj = locationArray.getJSONArray(j); should be j_obj = locationArray.getJSONObject(j);, as the object wrapped with curly braces represents a JSONObject and not a JSONArray
Edit: you may consider using obj.optString("location_id"); to avoid a potential problem

Related

Read JSON array without curly brackets

I'm trying to get the values from array in a JSON file. Array only has a square brackets and not curly brackets something like this:
"{"name_of_the_array: ["value_1", "value_2"]"}"
I found here some answer but it did not work for me the code of the answer is:
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.get(i).toString();
list.add(str);
}
the list is ArrayList list.
{} denotes a JSONObject so you have to create a JSONObject first. [] denotes a JSONArray. You have to fetch the JSONArray from JSONObject with key.
Use this code
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray array = jsonObject.getJSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.get(i).toString();
list.add(str);
}
Check this and this for parsing
try something like this
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.getJSONObject(i).getString("string_name");
list.add(str);
}
or you can try this too
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.getString("string_name");
list.add(str);
}

JAVA parsing inner JSON

I am trying to take "key" value in a JSON file. I tried many solutions but I couldn't handle this ,it constantly gives me "at videos of type org.json.JSONObject cannot be converted to JSONArray" error. How can I solve this. Thank you.
My JSON data;
{ videos: {
results:[
{
id: "56c4ccbfc3a3680d52000610",
iso_639_1: "en",
iso_3166_1: "US",
key: "nIGtF3J5kn8",
}
]
}
}
My code;
JSONArray Movie_List = Search_Results.getJSONArray("videos");
for (int i = 0; i < Movie_List.length(); i++)
{
JSONObject movie = (JSONObject) Movie_List.get(i);
JSONArray Movie_List1 = movie.getJSONArray("results");
for (int j = 0; j < Movie_List1.length(); j++)
{
JSONObject movie2 = (JSONObject) Movie_List1.get(j);
key = movie2.getString("key");
}
}
The error;
Try some like this.. Your mistake come because you try to parse videos to JSONArray but it is JSONObject...
JSONObject videos = Search_Results.getJSONObject("videos");
JSONArray result= videos.getJSONArray("results");
for (int i = 0; i < result.length(); i++)
{
JSONObject movie = result.getJSONObject(i);
String movieId = movie.getString("id");
//and other values .. same way
}

How to convert the json array to string array in android?

I am converting an json array to string array thats doesn't exist any tag. I tried with all tricks but not succeeded.
json array:-
"validValues":["01_Abacus","02_AlarmClock","03_Basketball","04_Beaker","55_Watch"]
Code for convert the json array to string values
if (jsonComponentObj.has(TAG_VALID_VALUES)) {
String value = jsonComponentObj.getString(TAG_VALID_VALUES);
Logs.e("value " + value);
if (!value.equals("null")) {
JSONArray jsonArray = jsonComponentObj
.getJSONArray(TAG_VALID_VALUES);
if (jsonArray != null) {
ArrayList<String> stringArray = new ArrayList<String>();
for (int j = 0; j < jsonArray.length(); j++) {
try {
JSONObject jsonObject = jsonArray
.getJSONObject(j);
stringArray.add(jsonObject.toString());
} catch (JSONException e) {
Logs.e("Exception: "+e.toString());
e.printStackTrace();
}
}
}
}
}
Exception:
org.json.JSONException: Value 01_Abacus at 0 of type java.lang.String cannot be converted to JSONObject
If anyone have idea. Please reply. Thanks in advance..
Because validValues JSONArray contain only Strings instead of JSONObject.so get all values from JSONArray as:
for (int j = 0; j < jsonArray.length(); j++) {
String str_value = jsonArray.optString(j);
stringArray.add(str_value);
}
Your json array contains Strings not json object.Therefore to get Strings from json array directly use getString(),So
Change
JSONObject jsonObject = jsonArray.getJSONObject(j);
stringArray.add(jsonObject.toString());
to
stringArray.add(jsonArray.getString(j));
or
stringArray.add(jsonArray.optString(j));
You should use optString(int) to coerce a string value from array. Using getString(int) would cause problems if ever the values in the array were not strings.
for (int j = 0; j < jsonArray.length(); j++) {
String value = jsonArray.optString(j);
stringArray.add(value);
}
If you want to give a default value, use optString(int, String).
try JSONObject jsonObject = jsonArray.getString(j);
instead of JSONObject jsonObject = jsonArray.getJSONObject(j);

Populate the JSON Array with data from the Arraylist in android

please help me. I am trying to put values on my json array from the values of the arraylist, but unfortunately, I don't know why, my json array only gets the last value of the arraylist. I expect an output of
[{"Id":"1"},{"Id":"2"},{"Id":"3"}]
but it gives me
[{"Id":"3"},{"Id":"3"},{"Id":"3"}]
Can you help me get out of this. Thanks in advance, here is my code:
try
{
JSONObject jObject = new JSONObject();
JSONObject brand = new JSONObject();
JSONObject consumerSegments = new JSONObject();
JSONArray jArrayReportUpload = new JSONArray();
try {
JSONArray jArraySubrands = new JSONArray();
JSONArray jArrayConsumerSegments = new JSONArray();
for (int i = 0; i < arraylist_SUBBRANDS.size(); i++)
{
brand.put("Id", _brands.get(i));
Log.d("Brand: " + i, _brands.get(i));
jArraySubrands.put(brand);
}
Log.d("JSONbrand", jArraySubrands.toString());
for (int j = 0; j < arraylist_SEGMENTS.size(); j++) {
consumerSegments.put("Id",_segments.get(j));
Log.d("Segment: " + j, _segments.get(j));
jArrayConsumerSegments.put(consumerSegments);
}
Log.d("JSONsegment", jArrayConsumerSegments.toString());
Put JSONObject consumerSegments = new JSONObject();
as the first statement inside
for (int j = 0; j < arraylist_SEGMENTS.size(); j++) {
and not where it currently is, you are just overwriting a memory block with the same values rather than creating a new instance to hold the new values everytime

How to deal with JSON that has no names in android

I'm retrieving the following JSON from a web service:
["test_w","\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3","\u062a\u0637\u0628
\u064a \u0642 \u0631\u0642\u0645 2","\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645
1","test_333","\u0645\u0639\u0631\u0641\u0629 \u0627\u0644\u062a\u0627\u0621 \u0627\u0644
\u0645\u0631\u0628\u0648\u0637\u0629 \u0648\u062a\u0646\u0648\u064a\u0646 \u0627\u0644
\u0636\u0645","\u0639\u0644\u0648\u0645","\u0648\u0631\u0642\u0629 \u0639\u0645\u0644"]
As you can see, it's not in name, value format; I tried accessing the JsonObject itself but it's always returning null:
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
notifArray.add(json_data.toString());
Is there any way to deal with this particular format of JSON ?
You have an array of strings. So you have to use getString (or optString) to access the array elements:
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
String str = jArray.getString(i);
notifArray.add(str);
}
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
notifArray.add(jArray.get(i));
}

Categories

Resources