How to Parse Json Array (Not Json Object) in Android

How to parse json array without json object title in android?

This is a JSONArray and not a JSONObject - to make a JSONObject from it, use

JSONObject jsonObject = jsonArray.getJSONObject(0);

this gets the first JSONObject from this JSONArray.

If you have multiple JSONObjects, use this:

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

To get the values:

jsonObject.getString("name");

How to parse JSONArray without JSONObject in Android

Suppose your values are in a array dataArray.

public JSONArray getJsonParam(){
JSONArray jsonArray=new JSONArray();

for(int i=0;i<dataArray.length();i++)
jsonArray.put(dataArray[i]);

return jsonArray;
}

call getJsonParam().toString(); where you need it.

JSON parsing without array name or node in Android

Try this:

 try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i <jsonArray.length() ; i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
textView.setText(jsonObject.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}

How to parse json array without json object title in android ? Help me

My goal whenever I do a lot of coding for answers if to make sure you actually understand what you are doing and not just copy and paste my code into your project. If you never worked with instanceof, I advise you to read up on it. If you have ANY questions, please ask me.

I will get you started. Why? You showing no code in your post, so I will explain what to do with this.

    JSONArray object = new JSONArray(jsonData);
//jsonData if the JSON String.
String configVal = null;

JSONArray configArray = null;
JSONArray timingArray = null;

boolean hasAreas = false;
boolean hasTiming = false;
try {
for (int i = 0; i < object.length(); i++) {
JSONObject getJSONObject = object.optJSONObject(i);

if (getJSONObject.get("configurationVal") instanceof JSONObject) {
JSONObject configValObject = getJSONObject.getJSONObject("configurationVal");
configArray = configValObject.getJSONArray("availableAreas");
hasAreas = true;
} else if (getJSONObject.get("configurationVal") instanceof JSONArray) {
timingArray = getJSONObject.getJSONArray("configurationVal");
hasTiming = true;
} else {
configVal = getJSONObject.getString("configurationVal");
}


String configName = getJSONObject.getString("configurationName");
int configId = getJSONObject.getInt("configurationId");

if (hasAreas) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId + ", AreaInformation: " + configArray);
}
if (hasTiming) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId + ", TimingInfo: " + timingArray);
}
if (!hasAreas && !hasTiming) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId);
}
//Reset these...
hasAreas = false;
hasTiming = false;
configVal = null;
}

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

This will print out the following.

ConfigVal: 2.5, ConfigName: CGST, ConfigId: 1
ConfigVal: 2.5, ConfigName: SGST, ConfigId: 2
ConfigVal: 0, ConfigName: DELIVERY_CHARGE, ConfigId: 3
ConfigVal: 9500100042,8939404592, ConfigName: Admin_Mobile, ConfigId: 4
ConfigVal: 98, ConfigName: MIN_ORDER_AMOUNT, ConfigId: 5
ConfigVal: 3, ConfigName: Packing_charge, ConfigId: 6
ConfigVal: , ConfigName: available_areas, ConfigId: 7, AreaInformation: [{"area_name":"Velachery","pincode":"600042","area_id":"1"},{"area_name":"Nanganallur","pincode":"600061","area_id":"2"},{"area_name":"Adambakkam","pincode":"323546","area_id":"3"},{"area_name":"Madipakkam","pincode":"600091","area_id":"4"},{"area_name":"Perungudi","pincode":"600096","area_id":"5"},{"area_name":"Pallikarani","pincode":"600100","area_id":"6"}]
ConfigVal: , ConfigName: order_timing, ConfigId: 8, TimingInfo: [{"start_time":"17: 00: 00","end_time ":"03: 00: 00"}]

Now, has you can see, the available_areas and order_timing is not parsed. I did, however, give you the information in a JSONArray. So you can follow what I did and parse it out. However, I would use a model for this with a List<>. It would be easier to work with.

How to parse JSON Array (Not Json Object) in Android

use the following snippet to parse the JsonArray.

JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}

Android Studio: How to parse a JSON Object from Inside of a JSON Array?

By getting first node in array you have obtained address node so no need to run loop there, address object is not an array you can get values directly. Try something like this:

        JSONArray array = object.getJSONArray("pollingLocations");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String locationName = jsonObject.getJSONObject("address")
.getString("locationName");
Log.d("locationName", locationName);
JSONArray sourcesArray = jsonObject.getJSONArray("sources");

// run loop to get values from this array
for (int j = 0; j < sourcesArray.length(); j++){
JSONObject source = sourcesArray.getJSONObject(j);
String name = source.getString("name");
}
}

How to parse a JSON Array Object?

You should use a JSON library such as Gson to parse those objects for your.

Add on your gradle dependencies

  implementation 'com.google.code.gson:gson:2.8.5'

You can create objects like

data class JsonRootObject(val userdata: List<User>)

data class User(val iD:Int, val name:String)

And then parse your object

val myParsedObject = Gson().fromJson(jsonString, JsonRootObject::class.java);

Make sure that your field names are the same as your object being parsed, this also means case sensitive. If you want to change their mapping name you might want to use @SerializedName("id") annotation like

class User {

@SerializedName("id")
lateinit var id: Int

@SerializedName("my_name")
lateinit var name: String

}

Consider this answer to your second object

{
"userdata":[
{
"iD":1,
"name":"name1"
},
{
"iD":2,
"name":"name2"
},
{
"iD":3,
"name":"name3"
},
{
"iD":4,
"name":"name4"
}
]
}

and of course, all this is written in Kotlin.

Parse Json Array Inside Multiple Json Array with android

Your JSON String is composed of an array , which contains arrays, which contain objects. so

// parse json to an array
JSONArray array = new JSONArray(jsonString);
// loop through that array and get nested arrays
for (int i = 0; i < array.length(); i++) {
JSONArray subArray = array.getJSONArray(i);
// loop trhough those nested arrays to retrieve the objects
for (int j = 0; j < subArray.length(); j++) {
JSONObject obj = subArray.getJsonObject(j);
// parse the object properties...

}
}


Related Topics



Leave a reply



Submit