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);
}
Related
I have the following JSON struncture:
{
"schedule":{
"day":[
{
"id":"Monday",
"items":[
{
},
{
}
]
},
{
"id":"Tuesday",
"items":[
{
},
{
}
]
}
]
}
}
And what I basically want to do is reach the items array inside the day array which is inside the schedule object.
But whenever I try to get the second JSON array, I get getJSONArray
(int) in JSONArray cannot be applied to (java.lang.String).
JSONObject baseJsonResponse = new JSONObject(dayJSON);
JSONArray dayArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day").getJSONArray("items");
You should use Two for loop respect to JSONArray.
JSONObject obj = new JSONObject(success);
JSONObject JOBJ_Schedule = obj.getJSONObject("schedule");
JSONArray schedule_Array = JOBJ_Schedule.getJSONArray("day");
for (int i = 0; i < schedule_Array.length(); i++)
{
JSONObject jOBJ = schedule_Array.getJSONObject(i);
JSONArray jArray = jOBJ.getJSONArray("items");
for (int j = 0; j < jArray.length(); j++)
{
JSONObject jOBJNEW = jArray.getJSONObject(j);
}
}
Looks like day is a list of objects. So basically, you'd have to do something like getJSONArray("day").get(0).getJSONArray("items").
you can try this
try {
JSONObject baseJsonResponse = new JSONObject("dayJSON");
JSONObject schedule= baseJsonResponse.getJSONObject("schedule");
JSONArray day=schedule.optJSONArray("day");
for (int i=0; i<day.length(); i++) {
JSONObject data = day.getJSONObject(i);
String id = data.getString("id");
JSONArray items = data.getJSONArray("items");
for (int j = 0; j < items.length(); j++) {
JSONObject data2 = day.getJSONObject(i);
String str = data2.getString("YOurkey");
Log.e("categories", str);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
According to the JSON you provided, baseJsonResponse.getJSONObject("schedule").getJSONArray("day") will return you a JSONArray instead of JSONObject.
There is a "item" JSONArray inside each "day" JSONObject. < May be this is the reason.
You can try
for(int i = 0 ; i < baseJsonResponse.getJSONObject("schedule").getJSONArray("day").length() ; i++){
JSONArray itemArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day")
.getJSONObject(i).getJSONArray("items");
}
I cannot figure out why my parsing is not working, this is my JSON:
{
"fileVersion":"1.0",
"graves":[
{
"ID_grave":"1",
"ID_line":"1",
"sequence":"1",
"persons":[
{
"ID_person":"1",
"name":"Janez",
"surname":"Novak",
"dateBirth":"1956-08-11",
"dateDeath":"2014-02-12",
"important":"0",
"imp_desc":""
}
]
},
{
"ID_grave":"2",
"ID_line":"1",
"sequence":"2",
"persons":[
{
"ID_person":"2",
"name":"Mojca",
"surname":"Novak",
"dateBirth":"1953-02-13",
"dateDeath":"2012-04-08",
"important":"0",
"imp_desc":""
}
]
}
]
}
This code is working, when I want to get the first JSONObject:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
graveArrayList.add(grave.getString("ID_grave"));
}
But I would like to get the "persons" array in "graves" object. This should work but it's not, I am getting only the first persons array, where the name is Janez and not the second array where the name is Mojca:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
//The problem was because of the index i, you have to change to k and it will work
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
graves is a JSONArray and persons is a JSONArray into graves
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
}
}
}
Do your parsing as follows ,
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
Try This:
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
JSONObject grave= persons.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
//so on..
}
}
Ok only today I discovered that I am getting the data only from the first persons array and from the second where for example is a person with a name Mojca..I tried all the three given solutions but nothing works..
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);
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));
}
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