JSONobject - How to Get a Value

JSONObject - How to get a value?

String loudScreaming = json.getJSONObject("LabelData").getString("slogan");

Can't get value from JSONObject

Your value is a JSONObject, not a string. Your error message makes that quite clear. If you really want it as a string, use

String value = jsonRecord.get("Key").toString();

You can pass any object to System.out.println, not just strings, but to actually turn it to a string, you need to call toString() yourself.

However, if you're expecting an actual String as the Key, and not a JSONObject, then you should take a second look at your JSON, because you're doing something wrong.

UPDATE:

Okay, looking at your schema, I see the problem. Instead of mapping the keys to values directly, your JSON maps keys to objects which then contain values. So to get the array in the JSON you posted, instead of

value = jsonRecord.get("myArray")

you would use

JSONArray value = jsonRecord.getJSONObject("myArray").getJSONArray("array");

and for the string, you would use

String value = jsonRecord.getJSONObject("Item").getString("string");

How do I extract value from Json

see this code what i am used in my application

String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";

I retrieved like this

JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        
double coolness = json.getDouble( "coolness" );
int altitude = json.getInt( "altitude" );
JSONObject pilot = json.getJSONObject("pilot");
String firstName = pilot.getString("firstName");
String lastName = pilot.getString("lastName");

System.out.println( "Coolness: " + coolness );
System.out.println( "Altitude: " + altitude );
System.out.println( "Pilot: " + lastName );

JSONObject get value of first node regardless of name

Use JSONObject.keys() which returns an iterator of the String names in this object. then use these keys to retrieve values.

To get only first value:

 Iterator<String> keys = jsonObject.keys();
// get some_name_i_wont_know in str_Name
String str_Name=keys.next();
// get the value i care about
String value = json.optString(str_Name);

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);


Related Topics



Leave a reply



Submit