I have an object like this one:
"choice": {
"000": {
"id": "001",
"label": "test",
"status": "0"
},
"001": {
"id": "001",
"label": "test",
"status": "0"
},
"002": {
"id": "001",
"label": "test",
"status": "0"
},
"003": {
"id": "001",
"label": "test",
"status": "0"
},
"004": {
"id": "001",
"label": "test",
"status": "0"
}
},
How can I parse that object with Gson+Retrofit? Or generate a POJO? There is an easy way of doing this?
Many thanks!
Retrofit is a very popular HTTP client library for Android. Using Retrofit makes it easy to parse API response and use it in your application. It has built-in GSON converter that can automatically parse HTTP response into an Object or any other types in Java that can be used in your code.
Google's Gson library provides a powerful framework for converting between JSON strings and Java objects. This library helps to avoid needing to write boilerplate code to parse JSON responses yourself. It can be used with any networking library, including the Android Async HTTP Client and OkHttp.
public final class GsonConverterFactory extends Converter.Factory. A converter which uses Gson for JSON. Because Gson is so flexible in the types it supports, this converter assumes that it can handle all types.
The main idea that all that you have in choice json object is Map:
public class RootObject{
Map <String,ChoiceEntry> choice;
}
public class ChoiceEntry{
String id;
String label;
int status;
}
You can create a POJO by pasting that JSON code into this link: http://pojo.sodhanalibrary.com/.
You posted a snippet that isn't properly formatted. I believe you'll have a multi-class POJO, and that is tricky to work with for certain uses such as listviews.
Let me know how it goes. Retrofit's really nice to use, but extremely annoying to figure out!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With