简体   繁体   中英

How can i set this JSON with dynamic keys in POJO and get data from it in Android JAVA?

I am developing an e-commerce app and I am getting this JSON data from API.

{"status": 0, "data": {"stores": {"test-5": {"name": "Test 5", "locality": {"name": "Some place B", "id": 2}, "cover": "IMAGE-URL-HERE"}, "test-2": {"name": "Test 2", "locality": {"name": "Some place A", "id": 2}, "cover": "IMAGE-URL-HERE"}}}, "action": [["DATA", "stores"]]}

I have created some POJO for this data too

public class PartnerStoreMainPOJO {
    @SerializedName("partnerstore")
    @Expose
    private PartnerStoresPOJO partnerstore;
    /**
     *
     * @return
     * The data
     */
    public PartnerStoresPOJO getPartnerStore() {
        return partnerstore;
    }
    /**
     *
     * @param partnerstore
     * The data
     */
    public void setPartnerStore(PartnerStoresPOJO partnerstore) {
        this.partnerstore = partnerstore;
    }
}

//------------- public class PartnerStoresPOJO {

@SerializedName("partnerstoredetail")
@Expose
private Map<String, PartnerStoreDetailPOJO> partnerstoredetail;

/**
 *
 * @return
 * The feeds
 */
public Map<String, PartnerStoreDetailPOJO>  getpartnerstoredetail() {
    return partnerstoredetail;
}
/**
 *
 * @param partnerstoredetail
 * The feeds
 */
public void setpartnerstoredetail(Map<String, PartnerStoreDetailPOJO> partnerstoredetail) {
    this.partnerstoredetail = partnerstoredetail;
}

} //----------------

public class PartnerStoreDetailPOJO {

@SerializedName("partnerstorelocality")
@Expose
private Map<String, PartnerStoreLocalityPOJO> partnerstorelocality;


@SerializedName("cover")
@Expose
private String cover;
@SerializedName("name")
@Expose
private String name;

/**
 * @return The name
 */
public String getName() {
    return name;
}

/**
 * @param name The name
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return The cover
 */
public String getCover() {
    return cover;
}

/**
 * @param cover The address
 */
public void setCover(String cover) {
    this.cover = cover;
}

public Map<String, PartnerStoreLocalityPOJO> getpartnerstorelocality() {
    return partnerstorelocality;
}

public void setpartnerstorelocality(Map<String, PartnerStoreLocalityPOJO> partnerstorelocality) {
    this.partnerstorelocality = partnerstorelocality;
}

}

//----------------

public class PartnerStoreLocalityPOJO {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("id")
    @Expose
    private String id;
    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }
    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }
    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }
}

//---------------

Amd i am using volley library. This is my java code-

public void onResultReceived(String response, String tag_json_obj) {
        if (tag_json_obj.equals(LOCALITY_SET)){

            try {
                JSONObject jsonObject=new JSONObject(response);
                String data=jsonObject.getString("data");

            } catch (JSONException e) {
                Log.d("EXCEPTN",e.toString());
                e.printStackTrace();
            }

        }
    }

I am using that data string.

Try it once: Do changes accordingly, It can give you a direction for your query.

public void convertJSON(JSONObject jsonObject) {
        try {
            JSONObject object = jsonObject.getJSONObject("data");

            Iterator<String> iter = object.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                Object value = object.get(key);

                JSONObject obj2 = object.getJSONObject(key);
                //set key to POJO

                Iterator<String> iter2 = obj2.keys();
                while (iter2.hasNext()) {
                    String key2 = iter2.next();
                    //....so on
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM