How to parse JSON object from a website into an arraylist in android

后端 未结 5 1086
广开言路
广开言路 2021-01-07 09:55

How can I parse a JSON object from a weblink into Android and store the different values into ArrayLists?

The JSON object of users looks like the below. It comes fro

5条回答
  •  萌比男神i
    2021-01-07 10:23

    Assuming you have a class that can store the results and appropiate constructor you can use code like below, using this json.org library for Java,

    import org.json.*;
    
    ArrayList users = new ArrayList();
    
    JSONObject o = new JSONObject(jsonString);
    JSONArray arr = obj.getJSONArray("Users");
    for (int i = 0; i < arr.length(); i++)
    {
       JSONObject user = arr.getJSONObject(i);
    
       users.add(new Users(user.getString("name"),user.getDouble("lat"),user.getDouble("lon")));  
    
       }
    
    }
    

提交回复
热议问题