简体   繁体   English

ServiceStack.Text错误的Json解析

[英]ServiceStack.Text wrong Json Parsing

I am tryng to parse a json comming from MtGox ticker. 我正在尝试解析来自MtG​​ox代码的json。 http://data.mtgox.com/api/2/BTCUSD/money/ticker http://data.mtgox.com/api/2/BTCUSD/money/ticker

I have attempted two ways with same result. 我已经尝试了两种方法获得相同的结果。

  1. JsonObject d1 = JsonSerializer.DeserializeString(downloadeddata) JsonObject d1 = JsonSerializer.DeserializeString(下载的数据)
  2. JsonObject d2 = JsonObject.Parse(downloadeddata) JsonObject d2 = JsonObject.Parse(下载的数据)

When I try to access the d1["data"] it appears to be perfect string for deserialization => 当我尝试访问d1 [“ data”]时,它似乎是反序列化的完美字符串=>

{
    [data, {
            "high" : {
                "value" : "600.00000",
                "value_int" : "60000000",
                "display" : "$600.00",
                "display_short" : "$600.00",
                "currency" : "USD"
            },
            "low" : {
                "value" : "515.00000",
                "value_int" : "51500000",
                "display" : "$515.00",
                "display_short" : "$515.00",
                "currency" : "USD"
            },
            "avg" : {
                "value" : "557.60317",
                "value_int" : "55760317",
                "display" : "$557.60",
                "display_short" : "$557.60",
                "currency" : "USD"
            },
            "vwap" : {
                "value" : "554.60404",
                "value_int" : "55460404",
                "display" : "$554.60",
                "display_short" : "$554.60",
                "currency" : "USD"
            },
            "vol" : {
                "value" : "20623.02466981",
                "value_int" : "2062302466981",
                "display" : "20,623.02\u00a0BTC",
                "display_short" : "20,623.02\u00a0BTC",
                "currency" : "BTC"
            },
            "last_local" : {
                "value" : "527.00000",
                "value_int" : "52700000",
                "display" : "$527.00",
                "display_short" : "$527.00",
                "currency" : "USD"
            },
            "last_orig" : {
                "value" : "527.00000",
                "value_int" : "52700000",
                "display" : "$527.00",
                "display_short" : "$527.00",
                "currency" : "USD"
            },
            "last_all" : {
                "value" : "527.00000",
                "value_int" : "52700000",
                "display" : "$527.00",
                "display_short" : "$527.00",
                "currency" : "USD"
            },
            "last" : {
                "value" : "527.00000",
                "value_int" : "52700000",
                "display" : "$527.00",
                "display_short" : "$527.00",
                "currency" : "USD"
            },
            "buy" : {
                "value" : "525.50002",
                "value_int" : "52550002",
                "display" : "$525.50",
                "display_short" : "$525.50",
                "currency" : "USD"
            },
            "sell" : {
                "value" : "526.99999",
                "value_int" : "52699999",
                "display" : "$527.00",
                "display_short" : "$527.00",
                "currency" : "USD"
            },
            "item" : "BTC",
            "now" : "1392201806575324"
        }
    ]
}

And the when I try to deserialize the latter string with either of above two options i get this 当我尝试使用上述两个选项之一反序列化后一个字符串时

JsonObject d3 = JsonObject.Parse(d1["data"]);

Count = 5
    [0]: {[high:{value:600.00000, value_int:60000000]}
    [1]: {[display:$600.00, display_short:$600.00]}
    [2]: {[currency:USD}, low:{value:515.00000]}
    [3]: {[value_int:51500000, display:$515.00]}
    [4]: {[display_short:$515.00, currency:USD]}

which is far from the truth. 这与事实相去甚远。 And according to me this result is wrong and even not json parsable. 根据我的说法,这个结果是错误的,甚至不是json可解析的。 => {[currency:USD}, low:{value:515.00000]} => {[currency:USD},最低:{value:515.00000]}

MS Javascript serializer is working ok. MS Javascript序列化程序运行正常。

So what am I doing wrong? 那我在做什么错?

Thanks 谢谢

How about deserializing to concrete classes. 如何反序列化具体类。

var ticker = ServiceStack.Text.JsonSerializer
                         .DeserializeFromString<Ticker.RootObject>(json);

public class Ticker
{
    public class Value
    {
        public string value { get; set; }
        public string value_int { get; set; }
        public string display { get; set; }
        public string display_short { get; set; }
        public string currency { get; set; }
    }

    public class Data
    {
        public Value high { get; set; }
        public Value low { get; set; }
        public Value avg { get; set; }
        public Value vwap { get; set; }
        public Value vol { get; set; }
        public Value last_local { get; set; }
        public Value last_orig { get; set; }
        public Value last_all { get; set; }
        public Value last { get; set; }
        public Value buy { get; set; }
        public Value sell { get; set; }
        public string item { get; set; }
        public string now { get; set; }
    }

    public class RootObject
    {
        public string result { get; set; }
        public Data data { get; set; }
    }
}

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

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