如何将网站中的JSON对象解析为android中的arraylist [英] How to parse JSON object from a website into an arraylist in android

查看:323
本文介绍了如何将网站中的JSON对象解析为android中的arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将网络链接中的JSON对象解析为Android并将不同的值存储到ArrayLists中?

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

用户的JSON对象如下所示。它来自一个网站。

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"}]}

我想解析到Android并存储不同的值,如name,lon和lat到数组列表。

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

我的最终目标是使用JSON对象在地图活动上放置标记。

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

推荐答案

使用 GSON 库来解析 JSON 到java类。使用起来非常简单。

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

生成的模型示例:

   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;

   }

这篇关于如何将网站中的JSON对象解析为android中的arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆