Reading Data from Nested Json Object Using Java

Get data from nested JSON Object in Java Android

You can use keys() iterator of json object & loop on it using while (keys.hasNext())

For your example, it would look something like this:

private void parseJson(String response) {
try {
JSONArray jsonArray = new JSONArray(response);

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

JSONObject jo = jsonObject.getJSONObject("fields");

Iterator<String> keys = jo.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject jo1 = jo.getJSONObject(key);
String f_name1 = jo1.getString("name");
Log.d("Field1.", f_name1);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}

How to get specific value from nested JSON object?

From the following class: JSONResponse

Not sure if you were using a library, or has a POJOs called JSONResponse

Also, you're calling the JSONResponse#getData, it means there's a getter called getData

presumably, your project already has a POJO for the attributes inside "data": [ { ... }, { ... } ] objects

E.g:

public class JSONResponse {
private List<Data> data;
private String success;

// getter & setter omitted
}

public class Data {

private Integer id;
private String nama;
private Integer harga;
private String deskripsi;
private Integer gambarid;
private Boolean paketan;
private String fasilitas;
private Integer totalTiket;
private Integer minTiket;
private String createdAt;
private String updatedAt;
private Object deletedAt;
private Integer gambarId;
private Gambar gambar;

// getter and setter omitted

}

public class Gambar {

private Integer id;
private String url;
private Object isGallery;
private String createdAt;
private String updatedAt;
private Object deletedAt;

// getter and setter omitted

}

Generate POJOs from JSON: https://www.jsonschema2pojo.org/

Or you can use plugins: https://plugins.jetbrains.com/plugin/8533-json2pojo

Here's the alternative approach might works for your usecases:

@override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response)
{
JSONResponse jsonResponse = response.body();
wisataList = new ArrayList<Data>();

Intent intent = getIntent();
int id = intent.getIntExtra(HomeActivity.EXTRA_NUMBER, 0);
for (int i = 0; i<jsonResponse.getData().length; i++)
{
Data wisata = jsonResponse.getData().get(i);
if (id == wisata.getId())
{
wisatList.add(wisata);
// break; // optional if you want to stop iterating, if the wisata already found
}
}
PutDataIntoRecyclerView(wisataList);

Retrieving values from nested JSON Object

Maybe you're not using the latest version of a JSON for Java Library.

json-simple has not been updated for a long time, while JSON-Java was updated 2 month ago.

JSON-Java can be found on GitHub, here is the link to its repo: https://github.com/douglascrockford/JSON-java

After switching the library, you can refer to my sample code down below:

public static void main(String[] args) {
String JSON = "{\"LanguageLevels\":{\"1\":\"Pocz\\u0105tkuj\\u0105cy\",\"2\":\"\\u015arednioZaawansowany\",\"3\":\"Zaawansowany\",\"4\":\"Ekspert\"}}\n";

JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject("LanguageLevels");
Object level = getSth.get("2");

System.out.println(level);
}

And as JSON-Java open-sourced, you can read the code and its document, they will guide you through.

Hope that it helps.

How to get data from nested JSON in Java Android

The 'name' value that you'll get will depend on how you are querying the JsonObject. You can read about the JsonObject definition and the query structure here https://developer.android.com/reference/org/json/JSONObject.html

Here's a quick stub on how you can retrieve specific property values from your example JSON.

JSONObject mainObject = new JSONObject(json_data_in_a_string);
JSONObject jsonAPI = mainObject.getJSONObject("jsonapi");
JSONArray array = (JSONArray)jsonAPI.get("data");

for (int i = 0; i < array.length(); i++) {

JSONObject childObject = array.getJSONObject(i);
String id = childObject.getString("id");

JSONObject name = childObject.getJSONObject("name");
String name_en= name.getString("en");
String name_es = name.getString("es");

JSONObject company = childObject.getJSONObject("company");
String company_id = company.getString("id");
String company_name = company.getString("name");

}

You can also create POJO's to easily read values from the JSON. There are several tools available online on how to generate pojo classes, validate json, etc.

Here are few that I use -

http://www.jsonschema2pojo.org/

http://jsonviewer.stack.hu/

https://jsonlint.com/



Related Topics



Leave a reply



Submit