alex.rusa
alex.rusa

Reputation: 11

Android class structure for json with multiple elements

I have an Android application that gets a json from a http call and looks like this:

{
   "string_1":{
      "prop_1":"value",
      "prop_2":"value"
   },
   "string_2":{
      "prop_1":"value",
      "prop_2":"value"
   },
   ...
   "string_n":{
      "prop_1":"value",
      "prop_2":"value"
   }
}

Here's the java class I wrote to use the data in my code:

public class FooClass implements Serializable {
    private LinkedHashMap<String, FooObject> objectsMap;

    public LinkedHashMap<String, FooObject> getObjectsMap() {
        return objectsMap;
    }
}

where FooObject is:

public class FooObject implements Serializable {
    @SerializedName("prop_1")
    private String property1;

    @SerializedName("prop_2")
    private String property2;

    public String getProperty1() {
        return property1;
    }
    public String getProperty1() {
        return property1;
    }
}

But since my json doesn't have an element called "objectsMap" my linked hash map from FooClass is always null. What class structure should I use for this json structure? Thank you.

UPDATE: I have managed to solve my problem. I use retrofit 1.9, and make the method return a result of type LinkedHashMap and it solved my problem.

Upvotes: 1

Views: 96

Answers (3)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("string_1")
@Expose
private String1 string1;
@SerializedName("string_2")
@Expose
private String2 string2;
@SerializedName("string_n")
@Expose
private StringN stringN;

public String1 getString1() {
return string1;
}

public void setString1(String1 string1) {
this.string1 = string1;
}

public String2 getString2() {
return string2;
}

public void setString2(String2 string2) {
this.string2 = string2;
}

public StringN getStringN() {
return stringN;
}

public void setStringN(StringN stringN) {
this.stringN = stringN;
}

}
-----------------------------------com.example.String1.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class String1 {

@SerializedName("prop_1")
@Expose
private String prop1;
@SerializedName("prop_2")
@Expose
private String prop2;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

public String getProp2() {
return prop2;
}

public void setProp2(String prop2) {
this.prop2 = prop2;
}

}
-----------------------------------com.example.String2.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class String2 {

@SerializedName("prop_1")
@Expose
private String prop1;
@SerializedName("prop_2")
@Expose
private String prop2;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

public String getProp2() {
return prop2;
}

public void setProp2(String prop2) {
this.prop2 = prop2;
}

}
-----------------------------------com.example.StringN.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class StringN {

@SerializedName("prop_1")
@Expose
private String prop1;
@SerializedName("prop_2")
@Expose
private String prop2;

public String getProp1() {
return prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}

public String getProp2() {
return prop2;
}

public void setProp2(String prop2) {
this.prop2 = prop2;
}

}

Hope it helps and you can use http://www.jsonschema2pojo.org/

Upvotes: 1

savepopulation
savepopulation

Reputation: 11921

If you're using a SerializedName annotation in your Response class which you're trying to map, just delete it and map your Json Response to

public class FooClass implements Serializable {
    private Map<String, FooObject> objectsMap;

    public Map<String, FooObject> getObjectsMap() {
        return objectsMap;
    }
}

Upvotes: 0

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

Better and more feasible is to change your response structure and have an Array rather than string_1, string_2... string_n. If you can't do that you will have to iterate through all the possible keys and save the response something like this

jObject = new JSONObject(response);
Iterator<?> keys = jObject.keys();

while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( jObject.get(key) instanceof JSONObject ) {
         // do your stuff here
    }
}

Upvotes: 2

Related Questions