How To Get The Value Of Json Child In An Array
Solution 1:
You should use data model i.e standard way.
Main Pojo class
publicclassMyModel
{
private Amber[] Amber;
public Amber[] getAmber ()
{
return Amber;
}
publicvoidsetAmber(Amber[] Amber)
{
this.Amber = Amber;
}
@Overridepublic String toString()
{
return"[Amber = "+Amber+"]";
}
}
Amber.java
publicclassAmber
{
private Class Class;
private Info Info;
//Can not getClass name of this method becuase getClass method is define in Object class so make it just getClassi or anything else.public Class getClassi()
{
return Class;
}
publicvoidsetClass(Class Class)
{
this.Class = Class;
}
public Info getInfo()
{
return Info;
}
publicvoidsetInfo(Info Info)
{
this.Info = Info;
}
@Overridepublic String toString()
{
return"Amber [Class = "+Class+", Info = "+Info+"]";
}
}
Class.java
publicclassClass
{
private Adorable Adorable;
private IronAge IronAge;
public Adorable getAdorable()
{
return Adorable;
}
publicvoidsetAdorable(Adorable Adorable)
{
this.Adorable = Adorable;
}
public IronAge getIronAge()
{
return IronAge;
}
publicvoidsetIronAge(IronAge IronAge)
{
this.IronAge = IronAge;
}
@Overridepublic String toString()
{
return"Info [Adorable = "+Adorable+", IronAge = "+IronAge+"]";
}
}
IronAge.java
publicclassIronAge
{
private String name;
private String link;
public String getName()
{
return name;
}
publicvoidsetName(String name)
{
this.name = name;
}
public String getLink()
{
return link;
}
publicvoidsetLink(String link)
{
this.link = link;
}
@Overridepublic String toString()
{
return"IronAge [name = "+name+", link = "+link+"]";
}
}
Adorable.java
publicclassAdorable
{
private String name;
private String link;
public String getName()
{
return name;
}
publicvoidsetName(String name)
{
this.name = name;
}
public String getLink()
{
return link;
}
publicvoidsetLink(String link)
{
this.link = link;
}
@Overridepublic String toString()
{
return"Adorable [name = "+name+", link = "+link+"]";
}
}
Info.java
publicclassInfo
{
private String image;
private String name;
private String bio;
public String getImage()
{
return image;
}
publicvoidsetImage(String image)
{
this.image = image;
}
public String getName()
{
return name;
}
publicvoidsetName(String name)
{
this.name = name;
}
public String getBio()
{
return bio;
}
publicvoidsetBio(String bio)
{
this.bio = bio;
}
@Overridepublic String toString()
{
return" class [image = "+image+", name = "+name+", bio = "+bio+"]";
}
}
Now use GSON to parse JSON data to model class.
This is the Gradle dependency for GSON
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
publicvoidparse(String jsonString){
Gsongson=newGson();
MyModelmodel= gson.fromJson(jsonString, MyModel.class);
for(Amber amber : model.getAmber()){
//TO GET LinksStringlink1= amber.getClassi().getAdorable().getLink();
Stringlink2= amber.getClassi().getIronAge().getLink();
}
}
You can use this to convert JSON to model pojo classes.
Solution 2:
The Class
is JSOnObject
with multiple JSONObject's
inside, so once after getting Class
you need to iterate through each JSONObject
to get the link
value
JSONObject jsonObject2 = jsonObject.getJSONObject("Class");
Iterator<String> itr = jsonObject2.keys();
while(itr.hasNext()) {
JSONObject innerObj = jsonObject2.getJSONObject(itr.next());
String link = innerObj.optString("link");
}
Solution 3:
Please check the updated response of the above code. In my system, I successfully executed the code without crash and value output.
try {
String json="{\n"+" \"Amber\": [\n"+" {\n"+" \"Info\": {\n"+" \"name\": \"Amber\",\n"+" \"bio\": \"Turkish-Cypriot\",\n"+" \"image\": \"null\"\n"+" },\n"+" \"Class\": {\n"+" \"Adorable\": {\n"+" \"name\": \"\",\n"+" \"link\": \"\"\n"+" },\n"+" \"IronAge\": {\n"+" \"name\": \"\",\n"+" \"link\": \"\"\n"+" }\n"+" }\n"+" }\n"+" ]\n"+"}";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArrayAmber= jsonObject.getJSONArray("Amber");
for(int i=0; i<jsonArrayAmber.length(); i++) {
JSONObject jsonObject1= jsonArrayAmber.getJSONObject(i).getJSONObject("Info");
if(jsonObject1!=null) {
String bioValue= jsonObject1.getString("bio");
System.out.println("jsonTest "+bioValue);
}
JSONObject jsonObject2= jsonArrayAmber.getJSONObject(i).getJSONObject("Class");
if(jsonObject2!=null) {
JSONObject jsonObjectAdorable = jsonObject2.getJSONObject("Adorable");
if(jsonObjectAdorable!=null) {
String linkAdorable = jsonObjectAdorable.getString("link");
System.out.println("jsonTest "+linkAdorable);
}
JSONObject jsonObjectIronAge = jsonObject2.getJSONObject("IronAge");
if(jsonObjectIronAge!=null) {
StringIronAgeLink= jsonObjectIronAge.getString("link");
System.out.println("jsonTest "+IronAgeLink);
}
}
}
} catch (Exception e){
e.printStackTrace();
}
Solution 4:
Because some field names in your JSON string are not fixed, a better way is to covert it to a Map<String, Object>
and retrieve value hierarchically as following code snippet.
BTW, there two identical field name - link
- in JSON string, so I modify its value for identification as follows:
Modified JSON string
...
"Adorable": {
"name": "",
"link": "Adorable-link"
},
"IronAge": {
"name": "",
"link": "IronAge-link"
}
...
Code snippet
ObjectMapper mapper = newObjectMapper();
Map<String, Object> data = mapper.readValue(jsonStr, newTypeReference<HashMap<String, Object>>(){});
data.forEach((k,v) -> {
((List<Object>) v).forEach(e -> {
Map<String, Object> infoNode = (Map<String, Object>) ((Map<String, Object>) e).get("Info");
System.out.println("The value of bio: " + infoNode.get("bio").toString());
Map<String, Object> classNode = (Map<String, Object>) ((Map<String, Object>) e).get("Class");
classNode.forEach((k1,v1) -> {
System.out.println("The value of link in " + k1 + ": " + ((Map<String, Object>) v1).get("link").toString());
});
});
});
Console output
The value of bio: Turkish-Cypriot The value of link in Adorable: Adorable-link The value of link in IronAge: IronAge-link
Solution 5:
Simple and short answer, please try this one
try {
JSONObject objects=newJSONObject(object);
JSONArray array=objects.getJSONArray("Amber");
for(int j=0; j<array.length(); j++)
{
String bio=array.optJSONObject(j).optJSONObject("Info").optString("bio");
String linkClass=array.optJSONObject(j).optJSONObject("Class").optJSONObject("Adorable").optString("link");
String linkIronAge=array.optJSONObject(j).optJSONObject("Class").optJSONObject("IronAge").optString("link");
}
} catch (JSONException e) {
e.printStackTrace();
}
Post a Comment for "How To Get The Value Of Json Child In An Array"