[英]Parsing nested json objects in retrofit android
我無法解析翻新版的json,這是我的示例json,密鑰是動態的,此類json數據需要什么POJO定義:
{
"Fri Mar 23 2018 17:35:36 GMT+0530 (IST)": {
"PDF": "",
"URL": "",
"image": "",
"shortDescription": "test one",
"timestamp": "2018-03-23T12:05:36.319Z",
"title": "test "
},
"Fri Mar 23 2018 17:44:43 GMT+0530 (IST)": {
"PDF": "",
"URL": "",
"image": "",
"shortDescription": "two test",
"timestamp": "2018-03-23T12:14:43.194Z",
"title": "two"
},
"Fri Mar 23 2018 17:49:06 GMT+0530 (IST)": {
"PDF": "",
"URL": "https://twitter.com/CodingDoug/status/942576182276497409",
"image": "https://drive.google.com/open?id=1gBUaAVdAttmAqv68OChHCDqlbKAoxZW6",
"shortDescription": "test three",
"timestamp": "2018-03-23T12:19:06.835Z",
"title": "three"
},
"Mon Mar 26 2018 15:56:06 GMT+0530 (IST)": {
"PDF": "",
"URL": "https://www.goodmorningquote.com/inspirational-monday-quotes-start-happy/",
"image": "https://drive.google.com/open?id=1fg1z0_jzTUUiXhvkHyGLewJC2LFFzEyW",
"shortDescription": "1st day of week, great day to plan and jumpstart week ",
"timestamp": "2018-03-26T10:26:06.983Z",
"title": "Monday test 1 "
}
}
您可以創建如下所示的json解析類:
Gson gson = GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().create();
List<ModelClass> dataList= new ArrayList<>();
JSONObject issueObj = new JSONObject(jsonContent);
Iterator iterator = issueObj.keys();
while(iterator.hasNext()){
String key = (String)iterator.next();
JSONObject issue = issueObj.getJSONObject(key);
ModelClass model = gson.fromJson(issue.toString(), ModelClass.class);
dataList.add(model);
}
而您的模型類將是:
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ModelClass implements Serializable
{
@SerializedName("PDF")
@Expose
private String pDF;
@SerializedName("URL")
@Expose
private String uRL;
@SerializedName("image")
@Expose
private String image;
@SerializedName("shortDescription")
@Expose
private String shortDescription;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("title")
@Expose
private String title;
public String getPDF() {
return pDF;
}
public void setPDF(String pDF) {
this.pDF = pDF;
}
public String getURL() {
return uRL;
}
public void setURL(String uRL) {
this.uRL = uRL;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
嘗試使用Map<String, Object>
解析json。 字符串是變量鍵。 對象是您的“ PDF”,“ URL”,“圖像”,“ shortDescription”,“ timestamp”,“ title”的POJO:“ test”
當你得到JSON數據使改造對象,使POJO類,如果要生成POJO類是指這個網站.. http://www.jsonschema2pojo.org/
將您的json數據提供所有pojo類。
然后使改造對象如下..使Apiclient類像
public class ApiClient {
private final static String BASE_URL = "https://simplifiedcoding.net/demos/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
然后使API調用界面如下所示
public interface ApiInterface {
@GET("marvel/")
Call<List<Hero>> getHero(); // here pass your response pojo class name.
}
在主要活動中如何稱呼如下..
ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
Call<List<Hero>> call = apiInterface.getHero();
call.enqueue(new Callback<List<Hero>>() {
@Override
public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
}
@Override
public void onFailure(Call<List<Hero>> call, Throwable t) {
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.