-1

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.

Donato Micele
  • 15
  • 1
  • 5

4 Answers4

2

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

Community
  • 1
  • 1
Cris
  • 774
  • 9
  • 32
1

If you wanna learn JSON Parsing refer this

Parse mystring_json and with the help of either handler class you can add it to your list.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
1

You can try something like this

try{
JSONArray arr = new JSONArray(mystring_json);
List<Poi> ppp = new Arraylist<>();
for(int i=0;i<arr.length();i++){
Poi objj= new Poi();
JSONObject jObj= arr.getJSONObject(i);
objj.setId(jObj.getString("id"));
.
.
.

// at last add 
ppp.add(objj);
}
}Catch(JSONException e){
e.printstacktrace();
}
Mukeshkumar S
  • 785
  • 1
  • 14
  • 30
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
1

just add

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

in your dependency section of app.gradle file. And after that, at the time of parsing

Gson gson = new Gson();
PoiResponse obj = gson.fromJson(data, PoiResponse.class);
List<Poi> ppp = obj.mystring_json;

You have to create one more class, like

public class PoiResponse {
  public ArrayList<Poi> mystring_json;
}

above data is your String response from server.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30