Parse JSON Array Without Key in Android

Parse JSON Array without Key in Android

Have you tried doing this?

try {
// jsonString is a string variable that holds the JSON
JSONArray itemArray=new JSONArray(jsonString);
for (int i = 0; i < itemArray.length(); i++) {
int value=itemArray.getInt(i);
Log.e("json", i+"="+value);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Parse JSON Array without Key

Try this

try 
{
JSONObject resObject = new JSONObject("your json response");
JSONArray jsonArray = resObject.getJSONArray("rows");

for (int i = 0; i < jsonArray.length(); i++) {
JSONArray jsonArray1 = jsonArray.getJSONArray(i);

for (int j = 0; j < jsonArray1.length(); j++) {

Log.i("Value","->" +jsonArray1.getString(j));
}
}

}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

OUTPUT

Sample Image

Android: Get values from array without key from JSON file on server

FYI, "data" array is not inside "meta" and "view". It's on top at the same level where is "meta".

JSONArray jsonArray = response.getJSONArray("data");

Answer of second question : If you want to display first element of the data json array then do the following

JSONArray jsonArray = response.getJSONArray("data");
if (jsonArray!=null && jsonArray.length() > 0) {
String data = jsonArray.optString(0, null);
textView.setText(data);
}

Result is

Sample Image

How to fetch JSON array without any key in Retrofit?

To Parse such an Array, you can use JSONArray as :

//For kotlin
val jsonArray = JSONArray(yourArrayString)
//For Java
JSONArray jsonArray = new JSONArray(yourArrayString);

In case you are extracting it from a JSON response which also contain other objects then you can use JSONObject with it as:

val jsonObject = JSONObject(yourJSONResponse)
val yourJSONArray: String = jsonObject.getString(" Key of Your JSONArray ")
val jsonArray = JSONArray(yourJSONArray)

In this case, we are extracting the Array string from JSON response using its key and then parsing the string as JSONArray later.

Remember that, I've used JSONArray which is org.json.JSONArray instead of JsonArray which of GSON.

Parse JSON without key

Iterate your json without key as follows

try {
JSONArray jsonArray = new JSONArray("[{\"data1\":\"contents of data1\",\"data2\":\"contents of data 2\"},{\"data1\":\"contents of data1\",\"data2\":\"contents of data 2\"}]");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

Iterator<?> keys = jsonObject.keys();

while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println("Mykey: " + key + " value: " + jsonObject.getString(key));
}
}

} catch (JSONException e) {
e.printStackTrace();
}

Parse JsonArray without key in android

i have a json response but is this standard format?

It would be without the < at the beginning and the > at the end. So:

new JSONObject(result.toString().substring(1, result.length() - 2));

or more likely:

new JSONArray(result.toString().substring(1, result.length() - 2));

since the top level of it is an array.

Since you've removed the < and > you had originally:

Since the top level of it is an array, you probably want JSONArray, not JSONObject:

new JSONArray(result.toString());

Parse JSON Array without key using Dart

Just use json.decode as normal, for example:

List<dynamic> l = json.decode('[5, 10, 15, 20]');

It happens that the members of l will all be ints

Android JSON parsing array inside another array without key

it's a simple way :

String genres  = "[\"Drama\",\"Musical\"]";

genres = genres.replace("[", "");
genres = genres.replace("]", "");
genres = genres.replace("\"", "");

Log.i("MY_TAG",genres); // Drama,Musical

test it and use it !

Android Json Object put a Array JSON without key

Solution

JSONObject params = new JSONObject();
params.put("label", "any description");

JSONArray locationJsonArray = new JSONArray();
locationJsonArray.put(25.775296); // Replace by your latitude
locationJsonArray.put(-100.2636682); // Replace by your longitude
params.put("location", locationJsonArray);

// For debug purpose
Log.i("DEBUG", params.toString(4));

Result

{
"label": "any description",
"location": [
25.775296,
-100.2636682
]
}


Related Topics



Leave a reply



Submit