开发者

Deserializing multi-dimensional array GSON

开发者 https://www.devze.com 2023-03-13 17:59 出处:网络
I want to deserialize the following json data https://mtgox.com/code/data/getDepth.php. I keep getting an 开发者_如何学Pythonerror. Below is my code.

I want to deserialize the following json data https://mtgox.com/code/data/getDepth.php. I keep getting an 开发者_如何学Pythonerror. Below is my code.

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

My AskBids class look like:

public class AskBids {

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

 public AskBids(){}
}

The error is get is com.google.gson.JsonParseException: Expecting object found: "asks"

Any ideas? Thanks


The JSON linked in the original question contains over 2,800 JSON tokens, element names and values. Here is a small section of that example that maintains the exact same structure.

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

That said, I copy-pasted the deserialization code from the original question, using both the original JSON and the shorter version I pasted above, and the code ran without error as expected.

Based on the error message Expecting object found: "asks", I suspect that the readHTTPS(URL) method is not returning the correct result, in that it is not including the opening { of the JSON. If this is the problem, but for some reason readHTTPS(URL) cannot be fixed, you could always "fix" its output by just concatenating the missing character(s).

0

精彩评论

暂无评论...
验证码 换一张
取 消