| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
mync
9年前发布

android中Gson解析

最近在做android项目,用到了Gson解析,总结一下放在这里,方便以后使用


服务器端代码:

    package com.sdufe.domain;                /**        * @author lili.guo        *         *         2014-6-10下午2:20:31        */        public class People {                    private int id;            private String username;            private String address;                    /**            * 无参构造函数            */            public People() {                // TODO Auto-generated constructor stub            }                    public People(int id, String username, String address) {                this.id = id;                this.username = username;                this.address = address;            }                    public int getId() {                return id;            }                    public void setId(int id) {                this.id = id;            }                    public String getUsername() {                return username;            }                    public void setUsername(String username) {                this.username = username;            }                    public String getAddress() {                return address;            }                    public void setAddress(String address) {                this.address = address;            }                }  

    package com.sdufe.tools;                import com.google.gson.Gson;                /**        * @author lili.guo        *        * 2014-6-10下午2:39:47        */        public class JsonTools {                        /**            * 将普通的字符串转化为json            * @param value            * @return            */            public static String CreateJsonStringbyGson(String value){                Gson gson=new Gson();                String json=gson.toJson(value);                return json;            }                }  

    package com.sdufe.Service;                import java.util.ArrayList;        import java.util.HashMap;        import java.util.List;        import java.util.Map;                import com.sdufe.domain.People;                /**        * @author lili.guo        *        * 2014-6-10下午2:27:36        */        public class JsonService {                        public People getPeople(){                People people=new People(1001, "Thea", "beijing");                return people;            }                        public List<People> getList(){                List<People> list=new ArrayList<People>();                list.add(new People(1001, "Thea", "beijing"));                list.add(new People(1001, "Thea", "beijing"));                list.add(new People(1001, "Thea", "beijing"));                return list;            }                        public List<String> getString(){                List<String> list=new ArrayList<String>();                list.add("beijing");                list.add("shanghai");                return list;            }                        public List<Map<String, Object>> getListMap(){                List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();                Map<String, Object> map1=new HashMap<String, Object>();                map1.put("id", 1001);                map1.put("username", "Thea");                map1.put("address", "beijing");                list.add(map1);                Map<String, Object> map2=new HashMap<String, Object>();                map2.put("id", 1001);                map2.put("username", "Thea");                map2.put("address", "beijing");                list.add(map2);                return list;            }                }  

其中jsonService中转化的数据类型比较多,差不多经常用到的几种都放在这里了


客户端代码:

    package com.sdufe.tools;                import java.util.ArrayList;        import java.util.List;        import java.util.Map;                import com.google.gson.Gson;        import com.google.gson.reflect.TypeToken;        import com.sdufe.domain.People;                /**        * @author lili.guo        *         *         2014-6-10下午2:53:16        */        public class GsonTool {                    public static People getPeople(String jsonString) {                        Gson gson = new Gson();                People people = gson.fromJson(jsonString, People.class);                return people;            }                    public static List<People> getList(String jsonString) {                        Gson gson = new Gson();                List<People> list=new ArrayList<People>();                list=gson.fromJson(jsonString, new TypeToken<List<People>>() {                }.getType());                return list;            }                        public static List<String> getString(String json){                                Gson gson=new Gson();                List<String> list=new ArrayList<String>();                list=gson.fromJson(json, new TypeToken<List<String>>() {                }.getType());                return list;            }                        public List<Map<String, Object>> getListMap(String json){                List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();                Gson gson=new Gson();                list=gson.fromJson(json, new TypeToken<List<Map<String,Object>>>() {                }.getType());                return list;            }                }  

稍微解释一下

服务器端负责把String类型的字符串转化成json类型,而客户端则负责把json类型解析成普通的String类型

 本文由用户 mync 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1450672259323.html
Gson JSON开发包