How to parse JSON object from a website into an ar

2020-03-07 08:22发布

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 from a website.

{"Users":[{"name":"Kane","lon":"4.371645","lat":"31.396911"},
          {"name":"Sam","lon":"4.129737","lat":"31.194824"},
          {"name":"Ryan","lon":"4.023134","lat":"31.298222"},
          {"name":"Jerry","lon":"4.262276","lat":"31.295054"}]}

I want to parse in into Android and store the different values like name, lon and lat into an array list.

My ultimate goal is to use the JSON object to place markers on a map activity.

5条回答
Rolldiameter
2楼-- · 2020-03-07 08:41

Use GSON library to parse JSON to java class. It is very simple to use.

Gson gson = new Gson();
Response response = gson.fromJson(jsonLine, Users.class);

Generated model example:

   public class Users {

       @SerializedName("Users")
       @Expose
       public List<User> Users = new ArrayList<User>();
    }


    public class User {

       @SerializedName("name")
       @Expose
       public String name;

       @SerializedName("lon")
       @Expose
       public double lon;

       @SerializedName("lat")
       @Expose
       public double lat;

   }
查看更多
爷的心禁止访问
3楼-- · 2020-03-07 08:46

You can use the native JSONObject and JSONArray present on the Android SDK here : https://developer.android.com/reference/org/json/JSONObject.html

There is a method named JSONObject(String json) that allow you to do something like this (not tested) :

//creating your json object from your strong
JSONObject jsonObject = new JSONObject(yourString);
JSONArray users = jsonObject.getJSONArray("users");//this two lines throws a JSONException so put try{}catch{} block
for(JSON user in users)
{
        JSONObject currentUser = users.get(i);
        //insert yout user inside your ArrayList here
}
查看更多
老娘就宠你
4楼-- · 2020-03-07 08:47

You can create a Model class from your data model like:

public class DataTemplate{

public final List<User> Users;

public DataTemplate(List<User> Users){
    this.Users = Users;
}

public static class User{
    public final String name;
    public final double lon;
    public final double lat;

    public User(String name, double lon, double lat){
        //initialize elements
    }
} }

After that take a look at GSON library. Using that you can simply import the data as:

DataTemplate getTemplate(String path){
    try {
        return new Gson().fromJson(new InputStreamReader(dataFromURLRequest),  DataTemplate.class);
    } catch (Exception e) {}

After this, just directly retrieve the list from DataTemplate.

查看更多
看我几分像从前
5楼-- · 2020-03-07 08:58
DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(HttpUtil.BASIC_URL
                + HttpUtil.SUBSCRIPTION_URL);
        try{
            if (cookie != null) {
               // httpClient.setCookieStore(LoginJsonUtil.cookie);
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
                nameValuePair.add(new BasicNameValuePair("uid",
                        uid));
                nameValuePair.add(new BasicNameValuePair("subscriptionslist[pageindex]",
                        subscriptionslist_pageindex));
                nameValuePair.add(new BasicNameValuePair("subscriptionslist[recordlimit]",
                        subscriptionslist_recordlimit));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
查看更多
够拽才男人
6楼-- · 2020-03-07 09:02

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> users = new ArrayList<Users>();

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")));  

   }

}
查看更多
登录 后发表回答