Iterating through json objects received from the server

前端 未结 2 708
生来不讨喜
生来不讨喜 2021-01-25 05:20

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 a

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 05:57

    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

    if you need to separate the city from the name

    ArrayList name=new ArrayList();
    ArrayList city=new ArrayList();
    
    for(int i=0;i

提交回复
热议问题