简体   繁体   中英

Parsing json list when there is an exceptional objects as well using Retrofit2

Previously I was receiving the response like this:

在此处输入图片说明

I was parsing it like: Call<List<MyObject>> getList();

But now there are some new elements were added and the response changed to:

在此处输入图片说明

How to parse this object now? I searched my could not find any solutions.

This is how I am setting up my client.

在此处输入图片说明

This is the json object which i recieve as a response:

{"map":{"01":{"F":".","E":".","D":null,"C":null,"B":".","A":"."},"02":{"F":".","E":".","D":null,"C":null,"B":"Z","A":"."},"03":{"F":"A","E":"A","D":null,"C":null,"B":"A","A":"A"},"board":false,"type":{"num":"TT334","board":"WW","date":"31MAR","route":"AWETSW","pcount":""}}}

I dont

There are two potential solutions:

  1. You create a DTO. Gson will ignore fields you don't map in your dto. Your json doesn't use a list it is entirely objects.

  2. You manually parse the json using Gson's JsonReader

You can use a mixture of DTOs and manual parsing. I have done this for large json datasets and inconsistent datasets.

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