Donato Micele Donato Micele - 2 months ago 8
JSON Question

Android string JSON data parsing

I have a string with this data:

mystring_json = "[{"id":"a373e3e15bac1eeb001785b40a219d","dataInserimento":"2016-09-15 09:30:14","dataPubblicazione":"2016-09-15 09:29:00"},{"id":"4b2444b7b737c8d64e7d60c6515217","dataInserimento":"2016-09-15 08:32:04","dataPubblicazione":"2016-09-15 08:28:00"}...]";


I have a class Poi:

public class Poi {
public String id;
private String dataInserimento;
private String dataPubblicazione;
}


I create a 'Poi' list:

List<Poi> ppp = new ArrayList<>();


I have to put my_string_json data to 'ppp' list.

How can i do this?
I'm sorry for my english.

Answer

Posting an answer because I don't have enough reputation to leave a comment.

You can use gson library, so in order to parse you should do the following:

First, create the following class:

public class ArticleContainer {
    private List<Poi> lstPoi;
}

Then:

Gson gson = new Gson();
PoiResponse poiResponse = gson.fromJson(myString_json, PoiResponse.class);
List<Poi> ppp = poiResponse.getPoi();

In order to compile you need to add this line to your module's build.gradle file:

compile 'com.google.code.gson:gson:2.3.1'

For more information gson README and Related topic