android 用gson 解析数据出错!

 le__citron 发布于 2022-10-29 17:15

我用如下方法解析一个json字符串,但是就是运行都这就闪退了,不知道什么原因。

    protected void parseData(String result) {
        // TODO Auto-generated method stub
        Gson gson=new Gson();
        NewData data=gson.fromJson(result,NewData.class);
        System.out.println(data.toString());
         
    }

这是newData类

public class NewData{
    
      public ArrayList  food;
      
       public class kindFood{
            public String Description;
            public String PicUrl;
            public String Title;
            public String Url;
            
            @Override
            public String toString() {
                return "kindFood [Description=" + Description + ", PicUrl=" + PicUrl + ", Title=" + Title + ", Url="
                        + Url + "]";
            }

        }

}

这是json数据

[
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋热量(100克)",
        "Url": ""
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋    144 大卡",
        "Url": "http://i.boohee.com/mfood/view/910"
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋(土鸡)    138 大卡",
        "Url": "http://i.boohee.com/mfood/view/913"
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋(煮)    151 大卡",
        "Url": "http://i.boohee.com/mfood/view/1757"
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋白(鸡蛋清)    60 大卡",
        "Url": "http://i.boohee.com/mfood/view/914"
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋(白皮)    138 大卡",
        "Url": "http://i.boohee.com/mfood/view/911"
    },
    {
        "Description": "",
        "PicUrl": "",
        "Title": "鸡蛋(红皮)    156 大卡",
        "Url": "http://i.boohee.com/mfood/view/912"
    }
]
6 个回答
  • 需要使用到typetoken解析即可

    2022-10-31 01:14 回答
  • 你这个json数组的key是food么?而且最好把get set 方法设置了

    2022-10-31 01:15 回答
  • 使用gsonformat插件可以快速解决类似问题

    public class food{
        /**
         * Description :
         * PicUrl :
         * Title : 鸡蛋热量(100克)
         * Url :
         */
        private String Description;
        private String PicUrl;
        private String Title;
        private String Url;
    
        public String getDescription() {
            return Description;
        }
    
        public void setDescription(String Description) {
            this.Description = Description;
        }
    
        public String getPicUrl() {
            return PicUrl;
        }
    
        public void setPicUrl(String PicUrl) {
            this.PicUrl = PicUrl;
        }
    
        public String getTitle() {
            return Title;
        }
    
        public void setTitle(String Title) {
            this.Title = Title;
        }
    
        public String getUrl() {
            return Url;
        }
    
        public void setUrl(String Url) {
            this.Url = Url;
        }
    }
    

    然后在添加到arraylist

    2022-10-31 01:16 回答
  • 首先,将kindFood这个类提出来。
    其次,将NewData data=gson.fromJson(result,NewData.class);改成KindFood data=gson.fromJson(result,kindFood[].class);

    2022-10-31 17:39 回答
  • 试一下不要把kindFood作为内部类

    2022-10-31 17:39 回答
  • 你可以只保留KindFood类 里面注意有getter setter 方法 然后

    Gson gson = new Gson();
    List<KindFood> listFood = gson.fromJson(result,new TypeToken<List<KindFood>>(){}.getType());
    2022-10-31 17:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有