繁体   English   中英

在改造android中解析嵌套的json对象

[英]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.

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