• 解析JSON


    通过JSONObject解析

    private String parseJSONWithJSONObject(String string) {
        String returnData = "";
        try {
            JSONArray jsonArray = new JSONArray(string);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonCbject = jsonArray.getJSONObject(i);
                String id = jsonCbject.getString("id");
                String name = jsonCbject.getString("name");
                String version = jsonCbject.getString("name");
                returnData += id + name + version;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return returnData;
    }
    

    通过GSON解析

    //新建APP类
    public class APP {
        private int id;
        private String name;
        private String version;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getVersion() {
            return version;
        }
    
        public void setVersion(String version) {
            this.version = version;
        }
    
        public String getName() {
    
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    
    private Object parseJSONWithGSON(String string) {
        Gson gson = new Gson();
        //借助 TypeToken 将期望解析成的数据类型传入到 fromJson()方法中
        List<APP> appList = gson.fromJson(string, new TypeToken<List<APP>>() {}.getType());
        for (APP app : appList) {//foreach循环
            Log.d("MainActivity", "id is " + app.getId());
            Log.d("MainActivity", "name is " + app.getName());
            Log.d("MainActivity", "version is " + app.getVersion());
        }
        return null;
    }
  • 相关阅读:
    ZROI 19.08.04模拟赛
    具体数学 第一章 递归问题
    ZROI 19.08.02 杂题选讲
    win下在虚拟机安装CentOS 7 Linux系统
    ICP算法(迭代最近点)
    Python学习笔记(一)
    堆和堆排序
    笔试面试题记录-
    笔试面试记录-字符串转换成整型数等(aatoi,itoa)
    支持向量机原理(一) 线性支持向量机
  • 原文地址:https://www.cnblogs.com/cenzhongman/p/6406552.html
Copyright © 2020-2023  润新知