-1

im new here and have a problem, surprise surprise :D

I have a JSON String and i want to convert it into a List.

My JSON String:

{
"results": [
    {
        "uri": "http://xxxxxx",
        "downloadCount": 0,
        "lastDownloaded": "2017-04-10T16:12:47.438+02:00",
        "remoteDownloadCount": 0,
        "remoteLastDownloaded": "1970-01-01T01:00:00.000+01:00"
    },
    {
        "uri": "http://yyyyyyy",
        "downloadCount": 0,
        "lastDownloaded": "2017-04-10T16:12:47.560+02:00",
        "remoteDownloadCount": 0,
        "remoteLastDownloaded": "1970-01-01T01:00:00.000+01:00"
    },]}

How can i convert it in Java?

EDIT: My Problem was the "results" Root-Element... this worked fine.

Juli
  • 1
  • 1
  • 1
    It cannot be converted to a list. The JSON is an object with a field `results` which keeps a list of results objects. – StanislavL Sep 05 '17 at 13:33
  • 2
    The easiest way to work with JSON data in java is to use one of the existing excellent java libraries like Jackson to manipulate the JSON data. – jtahlborn Sep 05 '17 at 13:41
  • 1
    when you have a problem, google first before posting question on SO. As @jtahlborn said there are excellent libraries like Jackson, GSON. – want2learn Sep 05 '17 at 13:51
  • 1
    https://stackoverflow.com/questions/30594660/why-cant-i-unwrap-the-root-node-and-deserialize-an-array-of-objects This worked for me: `ObjectReader objectReader = objectMapper.reader(new TypeReference>(){}).withRootName("results"); List artifactList = objectReader.readValue(jsonString);` – Juli Sep 05 '17 at 14:06

2 Answers2

1

First you need to make a Java model object which matches the model in your JSON e.g.:

public class MyClass {
    private String uri;
    private int downloadCount;
    private ZonedDateTime lastDownloaded;
    private int remoteDownloadCount;
    private ZonedDateTime remoteLastDownloaded;

   (getters and setters)

}

Then you can use a JSON parser like Jackson (https://github.com/FasterXML/jackson) to parse your JSON as a list of instances of this object using the Jackson ObjectMapper class (https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html):

ObjectMapper objectMapper = new ObjectMapper();
MyClass[] myClasses = objectMapper.readValue(jsonString, MyClass[].class);
Plog
  • 9,164
  • 5
  • 41
  • 66
  • i tried it before and i get this error: Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of de.vsa.resttest.artifact[] out of START_OBJECT token at [Source: java.io.StringReader@351d0846; line: 1, column: 1] – Juli Sep 05 '17 at 13:57
  • I think your JSON is malformed. The last comma shouldnt be there – Plog Sep 05 '17 at 14:26
  • @Juli ObjectMapper will not convert a String to ZonedDateTime directly. You can make the datatype as String for `lastDownloaded` and `remoteLastDownloaded` to avoid the `JsonMappingException`. Refer my answer for the same. – Sridhar Sep 05 '17 at 14:29
1

Create a class for accessing data.

class ListElement {
    public String uri;
    public int downloadCount;
    public String lastDownloaded;
    public int remoteDownloadCount;
    public String remoteLastDownloaded;
}

Then, parse the json and get the list and convert it to list.

public static void main(String[] args) throws ParseException {
    Gson gson = new Gson();
    JsonElement list = new JsonParser().parse(json).getAsJsonObject().get("results");
    List<ListElement> listObj = gson.fromJson(list, new TypeToken<List<ListElement>>() {}.getType());
    System.out.println(listObj.size());
}

Note that I used String instead of ZonedDateTime. Since, its a String(enclosed between quotes) for JsonObject.

Sridhar
  • 1,518
  • 14
  • 27