简体   繁体   中英

Dynamic JSON parsing in android Retrofit?

i am using retofit for calling the API in android.one of my API gives dynamic data,how to parse that data?

{   
"status":1,
   "data":[      
         {
         "dynamic_key":65299,
         "dynamic_key":"Nagavati Murugeshan",
         "dynamic_key":"delhi"

        }

],
   "SurveyTemplateID":"5",
   "UID":"65299"
}

You can use Map

import java.util.List; 

import com.google.gson.annotations.SerializedName;
import com.google.gson.internal.LinkedTreeMap;

public class Response {

    @SerializedName("UID")
    private String uID;

    @SerializedName("data")
    private List<LinkedTreeMap<String, String>> data;

    @SerializedName("SurveyTemplateID")
    private String surveyTemplateID;

    @SerializedName("status")
    private int status;

    public void setUID(String uID) {
        this.uID = uID;
    }

    public String getUID() {
        return uID;
    }

    public void setData(List<LinkedTreeMap<String, String>> data) {
        this.data = data;
    }

    public List<LinkedTreeMap<String, String>> getData() {
        return data;
    }

    public void setSurveyTemplateID(String surveyTemplateID) {
        this.surveyTemplateID = surveyTemplateID;
    }

    public String getSurveyTemplateID() {
        return surveyTemplateID;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }
}

And as you can see in debug image,the map will have the key value of your dynamic data.

It's important that your dynamic keys to be unique or your json format will not be correct and app will crash with duplicate key error

在此处输入图像描述

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