Reputation: 50
How to get the particular data object from the JSON data from the given JSON data:
{
"customer":{
"id":1117198024800,
"email":"[email protected]",
"accepts_marketing":false
}
}
I need to parse ID from the above data, Can anyone help me please. Thanks in advance!!!
Upvotes: 0
Views: 118
Reputation: 12857
add to app/build.gradle:
dependencies {
...
implementation "com.google.code.gson:gson:2.8.1"
}
In code:
String string = "{
\"customer\":{
\"id\":1117198024800,
\"email\":\"[email protected]\",
\"accepts_marketing\":false
}
}";
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<HashMap<String,Customer>>() {}.getType();
HashMap<String, Customer> hashMap = new Gson().fromJson(string, type).
Customer customer = hashMap.get("customer");
Customer.java class:
public class Customer{
Long id;
String email;
Boolean acceptsMarketing;
}
Upvotes: 0
Reputation: 1802
Use this
JSONObject responceObj = new JSONObject(data);
JSONObject customer= response.getJSONObject("customer");
String id= customer.getString("id");
String email= customer.getString("email");
String accepts_marketing= customer.getString("accepts_marketing");
Upvotes: 1
Reputation: 76
If you will use it as string:
JSONObject reader = new JSONObject(data);
JSONObject customer = reader.getJSONObject("customer");
String id = (String) customer.getString("id");
Upvotes: 1