Peter
Peter

Reputation: 33

convert php array to array List

My jsonobject returns the following string from a php data source:

String[] faili =  new String [] {object.getString("slikaordfail")};
-->
["San Diego_3.jpg","San Diego_2.jpg","San Diego_1.jpg"]

How can I get an array in java from this string?

Tried something like

 List<String> stringList = new ArrayList<String>(Arrays.asList(faili));

but with the same result. It seems that the brackets from the php array are the problem. I don't like do delete them with string operations. There must be a more elegant way.

Debugger shows only one record:

0=  "["San Diego_3.jpg","San Diego_2.jpg","San Diego_1.jpg"]"

Any help will be highly appreciated.

Upvotes: 0

Views: 1626

Answers (1)

Anand Diamond
Anand Diamond

Reputation: 339

Use Gson library. Create custom class like this Details.

@SerializedName("slikaordfail")
private ArrayList<Details> eventDetailses;

public ArrayList<Details> getEventDetailses() {
    return eventDetailses;
}

public void setEventDetailses(ArrayList<Details> rentDetailses) {
    this.eventDetailses = rentDetailses;
}

 @SerializedName("event_pic")
private String eventPicture;

public String getEventPicture() {
    return eventPicture;
}

public void setEventPicture(String eventPicture) {
    this.eventPicture = eventPicture;
}

And in your Activity call.

Details details = gson.fromJson(result, Details.class);//result is your json data
ArrayList<Details> detailses = details.getEventDetailses();//getImages using detailses arrayList 

Upvotes: 1

Related Questions