• android 数据传输 json xml


    学习android 记录  第一次写随笔 先试验下流程
    一 json 

    android 解析json 有多种  如json lib 等,还是应该采用官方吧gson,jsonreader(主要是sdk2.0,2.1,2.22没有jsonreader,其实是一样的),方便 简单 

     使用GSON 非常简单

    首先下载gson

    然后就可以直接使用了

    两个方法  通俗说

    tojson 类转json
    fromjson json转类

    直接上代码  很简单

    User 类

    public class User {
    private String name;
    private String address;
    private int age;
    private boolean sex;
    public boolean isSex() {
    return sex;
    }
    public void setSex(boolean sex) {
    this.sex = sex;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    @Override
    public String toString() {
    return "User [name=" + name + ", address=" + address + ", age=" + age
    + ", sex=" + sex + "]";
    }
    public User(String name, String address, int age, boolean sex) {
    super();
    this.name = name;
    this.address = address;
    this.age = age;
    this.sex = sex;
    }


    }

    用于测试用提供数据的类

    public class JsonData {
    public User getUser(){
    User user = new User("张三","北京市",18,true);

    return user;
    }
    public List<User> getUsers(){
    List<User> users = new ArrayList<User>();
    User user = new User("张三","北京市",18,true);
    users.add(user);
    user = new User("李四","南京市",19,false);
    users.add(user);
    return users;
    }
    public String getString(){
    User user = new User("张三","北京市",18,true);
    return user.toString();
    }
    public List<String> getStrings(){
    List<String> list = new ArrayList<String>();
    list.add("张三");
    list.add("李四");
    list.add("王五");
    return list;
    }
    public List<Map<String,String>> getlistmap(){
    List<Map<String,String>> list = new ArrayList<Map<String,String>>();
    Map<String,String> map = new HashMap<String, String>();
    map.put("name", "zhangsan");
    map.put("sex", "men");
    list.add(map);
    map = new HashMap<String, String>();
    map.put("name", "wangwu");
    map.put("sex", "women");
    list.add(map);
    return list;
    }
    }

    最后 main

    String jsonstr = gson.toJson(jd.getUser());
    User user = gson.fromJson(jsonstr, User.class);
    System.out.println("user :"+user);

    jsonstr = gson.toJson(jd.getUsers());
    ArrayList<User> users = gson.fromJson(jsonstr, new TypeToken<List<User>>(){}.getType());
    System.out.println("users :"+users);

    jsonstr = gson.toJson(jd.getStrings());
    List<String> list = gson.fromJson(jsonstr, new TypeToken<List<String>>(){}.getType());

    jsonstr = gson.toJson(jd.getlistmap());
    List<Map<String,String>> listmap = gson.fromJson(jsonstr, new TypeToken<List<Map<String,String>>>(){}.getType());

    基本上就这样 下次是xml解析  sax

  • 相关阅读:
    史上最简单的Hibernate入门简单介绍
    极客Web前端开发资源大荟萃
    ios7 UIScrollView 尺寸问题
    用ahk脚本自己主动删除flashcookies
    CSS文字样式
    Windows 10 安装
    万圣节福利:红孩儿3D引擎开发课程《3ds max导出插件初步》
    算法实验 层序列表问题(二叉树)
    git配置别名
    git忽略特殊文件
  • 原文地址:https://www.cnblogs.com/zhangxiaoshuai/p/4531908.html
Copyright © 2020-2023  润新知