繁体   English   中英

使用Gson将JSON反序列化为对象

[英]Deserialize JSON to object with Gson

我正在尝试将JSON反序列化为我的对象,但是我得到了一个NullPointerException 搜索之后,我发现问题可能出在数据结构中。

你能帮我创建一个合适的课程,或者发现我的错误吗?

[
  {
    "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"
      }
    ]
  }
]

这是我要填写的课程:

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;
    }
}

以下是我尝试初始化对象的方法:

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();
    }
}

但是当我试图打电话给tyres时我有一个NullPointerException (轮胎是一个类场)

  1. 您需要类中所有属性的getter和setter。

  2. 为类实现可序列化的接口。

你的json在“`”path_big“:path5”错了,

这是正确的Json: -

[
{
    "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-0000: 00: 00"
        }
    ]
},
{
    "id": "58",
    "brand": "Barum",
    "model": "Bravuris",
    "d": "13",
    "w": "7",
    "h": "0",
    "comment": "",
    "time_add": "2013-11-1413: 33: 13",
    "imgs": [
        {
            "id": "32",
            "reused_id": "58",
            "path_big": "path5",
            "path_small": "",
            "time_add": "0000-00-0000: 00: 00"
        }
    ]
}
]

请使用www.jsonlint.com验证您的JSON。

`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM