• 解析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;
    }
  • 相关阅读:
    【转】ListView,GridView之LayoutAnimation特殊动画的实现 ps:需要学习的是在getView中添加动画的思想
    自定义Dialog
    android 横向list特效——规格滑动
    android BaseAdapter优化
    自定义弧形进度条
    滑块闹钟界面
    HTML学习10
    HTML学习9
    HTML学习8
    HTML学习7
  • 原文地址:https://www.cnblogs.com/cenzhongman/p/6406552.html
Copyright © 2020-2023  润新知