【问题标题】:Deserializing multi-dimensional array GSON反序列化多维数组 GSON
【发布时间】:2011-06-18 18:43:50
【问题描述】:

我想反序列化以下json数据https://mtgox.com/code/data/getDepth.php。我不断收到错误消息。下面是我的代码。

Gson gson = new Gson();  
String json =  readHTTPS(new URL("https://mtgox.com/code/data/getDepth.php"));
AskBids askBids  = gson.fromJson(json, AskBids.class);   

我的 AskBids 类如下所示:

public class AskBids {

 private  String [] []   asks;
 private String [] []  bids;

 public AskBids(){}
}

错误是 get is com.google.gson.JsonParseException: Expecting object found: "asks"

有什么想法吗?谢谢

【问题讨论】:

    标签: java arrays json gson


    【解决方案1】:

    原始问题中链接的 JSON 包含超过 2,800 个 JSON 标记、元素名称和值。这是该示例的一小部分,它保持完全相同的结构。

    {
        "asks": [
            [
                18.22,
                15.362
            ],
            [
                25.4682,
                20
            ]
        ],
        "bids": [
            [
                18.06,
                50
            ],
            [
                18.0099,
                32.64
            ]
        ]
    }
    

    也就是说,我使用原始 JSON 和我上面粘贴的较短版本复制粘贴了原始问题中的反序列化代码,并且代码按预期运行而没有错误。

    根据错误消息Expecting object found: "asks",我怀疑readHTTPS(URL) 方法没有返回正确的结果,因为它不包括JSON 的开头{。如果这是问题所在,但由于某种原因无法修复 readHTTPS(URL),您始终可以通过连接缺失的字符来“修复”其输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2019-02-26
      • 2015-02-24
      • 2011-03-28
      • 1970-01-01
      • 2016-09-15
      相关资源
      最近更新 更多