D. Ister D. Ister - 1 month ago 20
JSON Question

gson model for array

I have a JSON file like following:

{
"count": 60,
"value": [{
"changesetId": 60,
"url": "http://...",
"author": {
"id": "...",
"displayName": "*...",
"uniqueName": "...",
"url": "http://...*
"imageUrl": "http://..."
},
"checkedInBy": {
"id": "...",
"displayName": "...",
"uniqueName": "...",
"url": "http://...",
"imageUrl": "http://..."
},
"createdDate": "2016-11-08T22:05:11.17Z",
"comment": "..."
},


I am stuck at the point to create a model to use the API Gson. I started like:

public class Changesets{
int count;
*TODO* // model for the JSON above.
}


A start for the model or the entire model would be much appreciated. I will use this to deserialize.

Edit: I tried;

public class Changesets {
int count;
int changeset;
String url;
Changeset.Author author;
Changeset.CheckedInBy checkedInBy;
String createdDate;
String comment;
}


Where I could successfully write Changeset model.

Answer

If you really need to model the respective Java classes, you will need to reverse engineering the JSON structure. In your case it will be something like this:

public class Changesets{
    int count;
    List<Change> value;
}

and I will let you complete the work.

However, if you only need an ad hoc Java object to deal with a complex JSON object in which you are only interested in a very specific property value, you can use the solution I suggested in this answer: Dynamic JSON structure to Java structure