反序列化JSON与GSON对象 [英] Deserialize JSON to object with Gson

查看:181
本文介绍了反序列化JSON与GSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反序列化到JSON我的对象,但我得到一个 NullPointerException异常。搜索之后,我发现这个问题也许在数据结构。

您可以帮我创建一个合适的类,或者被发现我的错误?

  [
  {
    ID:54
    品牌:库珀,
    模特:发现者^ h \\ / T加
    D:13,
    W:7,
    H:0,
    意见:测试1,
    time_add:2013年11月14日12时42分47秒,
    IMGS:[
      {
        ID:28,
        reused_id:54
        path_big:path5,
        path_small:,
        time_add:0000-00-00 00:00:00
      }
    ]
  },
  {
    ID:55,
    品牌:宝隆
    模特:Bravuris
    D:13,
    W:7,
    H:0,
    意见:ooooooopll
    time_add:2013年11月14日12时43分55秒,
    IMGS:[
      {
        ID:29,
        reused_id:55
        path_big:path5,
        path_small:,
        time_add:0000-00-00 00:00:00
      }
    ]
  },
  {
    ID:56,
    品牌:锦湖
    模特:锦湖KH17
    D:19,
    W:185
    H:50,
    意见:bugaga
    time_add:2013年11月14日13时14分58秒,
    IMGS:[
      {
        ID:30,
        reused_id:56
        path_big:path5,
        path_small:,
        time_add:0000-00-00 00:00:00
      }
    ]
  },
  {
    ID:57,
    品牌:宝隆
    模特:Bravuris
    D:13,
    W:7,
    H:0,
    意见:,
    time_add:2013年11月14日13点32分11秒,
    IMGS:[
      {
        ID:31,
        reused_id:57,
        path_big:path5
        path_small:,
        time_add:0000-00-00 00:00:00
      }
    ]
  },
  {
    ID:58,
    品牌:宝隆
    模特:Bravuris
    D:13,
    W:7,
    H:0,
    意见:,
    time_add:2013年11月14日13点33分13秒,
    IMGS:[
      {
        ID:32,
        reused_id:58,
        path_big:path5,
        path_small:,
        time_add:0000-00-00 00:00:00
      }
    ]
  }
]

这是我试图填补了类:

 公共类轮胎{
    公共轮胎(){    }
    公共字符串ID;
    公共字符串品牌;
    公共字符串模式;
    公共STRING D;
    公共字符串瓦;
    公共字符串H;
    公共字符串评论;
    公共字符串time_add;
    公众的ArrayList<图像及GT; IMGS;
    公共地图<字符串,字符串>获取信息() {
        地图<字符串,字符串>结果=新的HashMap<字符串,字符串>();
        result.put(ID,身份证);
        result.put(品牌,品牌);
        result.put(模型,模型);
        result.put(D,D);
        result.put(W,w)的;
        result.put(H中,h);
        result.put(注释,评论);
        result.put(time_add,time_add);
        返回结果;
    }
    公众的ArrayList<地图<字符串,字符串>> getImgsInfo(){
        ArrayList的<地图<字符串,字符串>>结果=新的ArrayList<地图<字符串,字符串>>();
        地图<字符串,字符串> imgInfo;
        对于(图像的img:IMGS){
            imgInfo = img.getInfo();
            result.add(imgInfo);
        }
        返回结果;    }}公共类图片{
    公众形象(){    }
    公共字符串ID;
    公共字符串reused_id;
    公共字符串path_big;
    公共字符串path_small;
    公共字符串time_add;
    公共地图<字符串,字符串>获取信息() {
        地图<字符串,字符串>结果=新的HashMap<字符串,字符串>();
        result.put(ID,身份证);
        result.put(reused_id,reused_id);
        result.put(path_big,path_big);
        result.put(path_small,path_small);
        result.put(time_add,time_add);        返回结果;
    }
}

和后续的是我如何努力来初始化对象:

 私人无效history_to_Tyres(字符串historyJSON){
    GSON GSON =新GSON();    类型Af - Ag型=(类型)新的ArrayList<&轮胎GT;();
    尝试{
        轮胎= gson.fromJson(historyJSON,Af - Ag型);
    }赶上(例外五){
        Toast.makeText(getBaseContext(),uncasted,Toast.LENGTH_SHORT).show();
    }
}

但我有一个 NullPointerException异常当我尝试打电话给轮胎(轮胎是一类场)


解决方案

  1. 您需要的getter和放大器; setter方法​​在类的所有属性。


  2. 实施有关的类Serializable接口。


I'm trying to deserialize a JSON into my object, but I get a NullPointerException. After search, I found that problem maybe in data struct.

Can you help me to create a suitable class, or maybe to spot my mistake?

[
  {
    "id": "54",
    "brand": "Cooper",
    "model": "Discoverer H\/T Plus",
    "d": "13",
    "w": "7",
    "h": "0",
    "comment": "test1",
    "time_add": "2013-11-14 12:42:47",
    "imgs": [
      {
        "id": "28",
        "reused_id": "54",
        "path_big": "path5",
        "path_small": "",
        "time_add": "0000-00-00 00:00:00"
      }
    ]
  },
  {
    "id": "55",
    "brand": "Barum",
    "model": "Bravuris",
    "d": "13",
    "w": "7",
    "h": "0",
    "comment": "ooooooopll",
    "time_add": "2013-11-14 12:43:55",
    "imgs": [
      {
        "id": "29",
        "reused_id": "55",
        "path_big": "path5",
        "path_small": "",
        "time_add": "0000-00-00 00:00:00"
      }
    ]
  },
  {
    "id": "56",
    "brand": "Kumho",
    "model": "Kumho KH17",
    "d": "19",
    "w": "185",
    "h": "50",
    "comment": "bugaga",
    "time_add": "2013-11-14 13:14:58",
    "imgs": [
      {
        "id": "30",
        "reused_id": "56",
        "path_big": "path5",
        "path_small": "",
        "time_add": "0000-00-00 00:00:00"
      }
    ]
  },
  {
    "id": "57",
    "brand": "Barum",
    "model": "Bravuris",
    "d": "13",
    "w": "7",
    "h": "0",
    "comment": "",
    "time_add": "2013-11-14 13:32:11",
    "imgs": [
      {
        "id": "31",
        "reused_id": "57",
        "path_big":path5",
        "path_small": "",
        "time_add": "0000-00-00 00:00:00"
      }
    ]
  },
  {
    "id": "58",
    "brand": "Barum",
    "model": "Bravuris",
    "d": "13",
    "w": "7",
    "h": "0",
    "comment": "",
    "time_add": "2013-11-14 13:33:13",
    "imgs": [
      {
        "id": "32",
        "reused_id": "58",
        "path_big": "path5",
        "path_small": "",
        "time_add": "0000-00-00 00:00:00"
      }
    ]
  }
]

This is the class I'm trying to fill:

public class Tyres {
    public Tyres() {

    }
    public String id;
    public String brand;
    public String model;
    public String d;
    public String w;
    public String h;
    public String comment;
    public String time_add;
    public ArrayList<Images> imgs;
    public Map<String, String> getInfo() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("id", id);
        result.put("brand", brand);
        result.put("model", model);
        result.put("d", d);
        result.put("w", w);
        result.put("h", h);
        result.put("comment", comment);
        result.put("time_add", time_add);
        return result;
    }
    public ArrayList<Map<String, String>> getImgsInfo() {
        ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
        Map<String, String> imgInfo;
        for(Images img: imgs) {
            imgInfo = img.getInfo();
            result.add(imgInfo);
        }
        return result;

    }

}

public class Images {
    public Images() {

    }
    public String id;
    public String reused_id;
    public String path_big;
    public String path_small;
    public String time_add;
    public Map<String, String> getInfo() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("id", id);
        result.put("reused_id", reused_id);
        result.put("path_big", path_big);
        result.put("path_small", path_small);
        result.put("time_add", time_add);

        return result;
    }
}

and the follow is how I try to initialize the object:

private void history_to_Tyres(String historyJSON) {
    Gson gson = new Gson();

    Type ttype = (Type) new ArrayList<Tyres>();
    try {
        tyres = gson.fromJson(historyJSON, ttype);
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "uncasted", Toast.LENGTH_SHORT).show();
    }
}

but I've got a NullPointerException when I try to call tyres (tyres is a class field)

解决方案

  1. You need getters & setters for all attributes in the classes.

  2. Implement serializable interface for the classes.

这篇关于反序列化JSON与GSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆