Android JSON Parsing of Multiple JSONobjects Inside JSONobject

Android JSON parsing of multiple JSONObjects inside JSONObject

here you can retrieve all your json data, ask for a specific key and innerKey to get what you want, cheers

    try
{
String jsonString="";//your json string here
JSONObject jObject= new JSONObject(jsonString).getJSONObject("categories");
Iterator<String> keys = jObject.keys();
while( keys.hasNext() )
{
String key = keys.next();
Log.v("**********", "**********");
Log.v("category key", key);
JSONObject innerJObject = jObject.getJSONObject(key);
Iterator<String> innerKeys = innerJObject.keys();
while( innerKeys.hasNext() )
{
String innerKkey = keys.next();
String value = innerJObject.getString(innerKkey);
Log.v("key = "+key, "value = "+value);
}
}
}
catch (JSONException e)
{ e.printStackTrace(); }

Parse multiple JSON objects in Android

Based on the response from the endpoint url that you added in comments the parsing would look like this

JSONObject forecast = new JSONObject(jsonData);

double latitude = forecast.getDouble("latitude");
double longitude= forecast.getDouble("longitude");
String timezone = forecast.getString("timezone");

JSONObject jsonObjCurrently= forecast.getJSONObject("currently");
//parse string, long and double objects within jsonObjCurrently accordingly

JSONObject jsonObjMinutely= forecast.getJSONObject("minutely");
String summary= jsonObjMinutely.getString("summary");
String icon= jsonObjMinutely.getString("icon");
JSONArray jsonArrayMinutelyData = jsonObjMinutely.getJSONArray("data");
for(int i=0; i<jsonArrayMinutelyData .length(); i++){
JSONObject tempData = jsonArrayMinutelyData.get(i);
long time = tempData.getLong("time");
//parse the remaining object pairs.
}

JSONObject jsonObjHourly= forecast.getJSONObject("hourly");
//similar to minutely parsing. Only has more and different data
JSONObject jsonObjDaily= forecast.getJSONObject("daily");
//similar to hourly parsing.
JSONObject jsonObjFlags= forecast.getJSONObject("flags");
//It has 5 array and 1 string object so parse accordingly.

I have added the parsing logic please save the data accordingly and use it.

How do I add multiple JSONObjects in JSON Parsing? I'm getting an error

Hey you need add below classes

public class Address {

@SerializedName("street")
@Expose
private String street;
@SerializedName("suite")
@Expose
private String suite;
@SerializedName("city")
@Expose
private String city;
@SerializedName("zipcode")
@Expose
private String zipcode;
@SerializedName("geo")
@Expose
private Geo geo;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getSuite() {
return suite;
}

public void setSuite(String suite) {
this.suite = suite;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getZipcode() {
return zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public Geo getGeo() {
return geo;
}

public void setGeo(Geo geo) {
this.geo = geo;
}

}

Company Class

public class Company {

@SerializedName("name")
@Expose
private String name;
@SerializedName("catchPhrase")
@Expose
private String catchPhrase;
@SerializedName("bs")
@Expose
private String bs;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCatchPhrase() {
return catchPhrase;
}

public void setCatchPhrase(String catchPhrase) {
this.catchPhrase = catchPhrase;
}

public String getBs() {
return bs;
}

public void setBs(String bs) {
this.bs = bs;
}

}

Geo Class

public class Geo {

@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;

public String getLat() {
return lat;
}

public void setLat(String lat) {
this.lat = lat;
}

public String getLng() {
return lng;
}

public void setLng(String lng) {
this.lng = lng;
}

}

Test Class

public class TestModel {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("address")
@Expose
private Address address;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("website")
@Expose
private String website;
@SerializedName("company")
@Expose
private Company company;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website = website;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

}

Replace below code with your code

@Note: Replace mJsonArray with your response class

ArrayList<TestModel> listdata = new ArrayList<TestModel>();
JSONArray mJsonArray = new JSONArray();

//Replace mJsonArray to response
for(int i=0; i<mJsonArray.length(); i++) {

try {
JSONObject json = mJsonArray.getJSONObject(i); //First object
String id = json.getString("id");
String name = json.getString("name");
String username = json.getString("username");
String email = json.getString("email");
String phone =json.getString("phone");
String website =json.getString("website");

JSONObject json2 = json.getJSONObject("address"); //Second object
String street = json2.getString("street");
String suite = json2.getString("suite");
String city = json2.getString("city");
String zipcode = json2.getString("zipcode");

JSONObject json3 = json2.getJSONObject("geo");
String lat = json3.getString("lat");
String lang = json3.getString("lng");

JSONObject json4 = json.getJSONObject("company");
String companyName = json4.getString("name");
String catchPhrase = json4.getString("catchPhrase");
String bs = json4.getString("bs");

TestModel model = new TestModel();
model.setId(id);
model.setName(name);
model.setUsername(username);
model.setEmail(email);
model.setPhone(phone);
model.setWebsite(website);

Address mAddress = new Address();
mAddress.setStreet(street);
mAddress.setSuite(suite);
mAddress.setCity(city);
mAddress.setZipcode(zipcode);

Geo mGeo = new Geo();
mGeo.setLat(lat);
mGeo.setLng(lang);

mAddress.setGeo(mGeo);

model.setAddress(mAddress);

Company company = new Company();
company.setBs(bs);
company.setCatchPhrase(catchPhrase);
company.setName(companyName);

model.setCompany(company);

listdata.add(model);

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

}

if(listdata.size()>0){
//Hope you need pass list to MyAdapter
listView.setAdapter(new Myadapter()); //Custom Adapter
}

JSON parsing to List view with multiple objects Android

Try this out:

try {
JSONObject object = (JSONObject) new JSONTokener(YOUR_JSON_STRING).nextValue();
String restaurentAddressId = object.getJSONArray("data").getJSONObject(0).getJSONObject("restaurentAddress").getString("restaurent_address_id");
String restaurentInfoId = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_info_id");
String restaurentBizOwnerName = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_business_owner_name");

}
catch (JSONException e) {
}

How to convert a Json object with multiple objects in it (legacy) to a Json array

Here's how you can do it:

// Extract the elements object
val elementsObject = jsonObject.getJSONObject("elements")

// Get a list of all the keys 1...10 (in your case)
val listOfKeys = elementsObject.keys()

// Then iterate over the keys and get the label object
listOfKeys.forEach { key ->
val labelObject = elementsObject.getJSONObject(key)
val labelValue = labelObject.getString("label")

print("$key:$labelValue") // 1:1fwef 2:2wefewfewf 3:wefwffe3 4:4 5:5 6:6 7:7 8:8 9:9 10:10
}

Android parse JSONObjects inside JSONObject

if your json string keys are dynamic then you can parse it as:

JSONObject root = new JSONObject(yourString);
// get towers JSONObject
JSONObject towers = root.getJSONObject("towers");
// get all Towers name from towers JSONObject

JSONArray alltowerslist=towers.names();

for(int i=0;i<alltowerslist.length();i++){
// get sub towers from towers
JSONObject sub_towers = towers.getJSONObject(alltowerslist.optString(i));
// get days list from sub_towers
JSONArray alldayslist=sub_towers.names();
for(int j=0;j<alldayslist.length();j++){
// get days from sub_towers
JSONObject days_json = sub_towers.getJSONObject(alldayslist.optString(j));

// get time json JSONObject from days_json
JSONArray alltimeslist=days_json.names();
for(int k=0;k<days_json.length();k++){
// get time from sub_towers
JSONObject time_json = days_json.getJSONObject(alltimeslist.optString(k));

// now get all value1 from time_json
JSONArray allvalelist=time_json.names();
for(int l=0;l<time_json.length();l++){
String str_value = time_json.optString(allvalelist.optString(l));
}
}
}
}


Related Topics



Leave a reply



Submit