Iterating through json objects received from the server

北城以北 提交于 2019-12-31 04:45:09

问题


I have a large group of json objects received from web server. I want to get all the data from all the json objects. For that How do I iterate through the json object so, that all the values can be stored on arraylist..

This is a sample model of my json object received from server. I need all the data (name and city) in two arraylists. For that how do I loop through the json objects. There is no way of getting the data as json array from the server. That's why I asked here. If it was json array, It would have been easier for me. So please help me..

[{"Name":"abin","City":"aa"},{"Name":"alex","City":"bb"},....... a large collection of json objects...]

回答1:


You could use Gson and parse the string to a java object.

For example you have a class.

public class Location{

    private String name;

    private String city;

    //getters and setters

}

and in your class you could just parse it to Location class

Gson gson=new Gson();

Location[] locations=gson.fromJson(jsonString,Location[].class);

after that you could loop through the locations

for(int i=0;i<locations.length;i++){

     System.out.println(locations[i].getName());

}

if you need to separate the city from the name

ArrayList name=new ArrayList();
ArrayList city=new ArrayList();

for(int i=0;i<locations.length;i++){

   name.add(locations[i].getName());
    city.add(locations[i].getCity());

}



回答2:


If you know the structure of your JSON String, then use google's Gson() (add the JAR to your project) to deserialize, in 3 easy steps:

  1. Create the Entity class (whatever your object is, I'm giving "Person" as example).

    public class Person {
    @Expose //this is a Gson annotation, tells Gson to serialize/deserialize the element
    @SerializedName("name") //this tells Gson the name of the element as it appears in the JSON     string, so it can be properly mapped in Java class
    private String name;
    @Expose
    @SerializedName("lastName")
    private String lastName;
    @Expose
    @SerializedName("streetName")
    private String streetName;
    //getters and setters follow
    }
    
  2. Create the class into which you deserialize the JSON string. In my example, the JSON string is actually an array of Persons.

    public class PersonsList extends ArrayList<Person> implements Serializable{
    //nothing else here
    }
    
    If the JSON string has a named key, then you don't have to extend ArrayList:
    
    public class PersonsList implements Serializable{
    @Expose
    @SerializedName("persons")
    private ArrayList<Person> persons;
    //getters / setters
    }
    
  3. Do the actual deserialization:

    String json = "[{person1},{person2},{person3}]";//your json here
    Gson gson = new Gson();
    PersonsList personsList = gson.fromJson(json, PersonsList.class);
    //then, depending on how you build PersonsList class, you iterate:
    for(Person p : personsList)//if you extended ArrayList
    //or
    for(Person p : personsList.getPersons())//if it's the second option
    


来源:https://stackoverflow.com/questions/25238186/iterating-through-json-objects-received-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!