2014-02-12 105 views
0

我tryng解析一個JSON從MtGox正在添加股票。 http://data.mtgox.com/api/2/BTCUSD/money/tickerServiceStack.Text錯誤的JSON解析

我曾試圖用同樣的結果有兩種方式。

  1. 的JSONObject D1 = JsonSerializer.DeserializeString(downloadeddata)
  2. 的JSONObject D2 = JsonObject.Parse(downloadeddata)

當我嘗試訪問D1 [ 「數據」]這似乎是完美串解串=>

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

而且當我嘗試用其中一種上面兩個選項反序列化後的字符串我得到這個

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

這是與事實相差太遠。而根據我的看法,這個結果是錯誤的,甚至不是json可解析的。 => {[貨幣:USD},低:{值:515.00000]}

MS的Javascript串行器工作確定。

那麼我做錯了什麼?

謝謝

+2

大家對同一主題的工作?這是我看到試圖解析* ticker *的第​​三個問題。有沒有辦法將答案發送給全班? – I4V

回答

1

如何反序列化到具體的類。

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

感謝您的回答。我已經做到了,它實際上工作得很好。但是,如果你看到我的問題的第二個代碼段「數= 5」和向下,你會看到它是正確解析的第一要素「高」,它是隻解析第一個元素。這是我的問題。 – ChristoD

+0

我希望看到類似[0]:{[高:{}]} [1]:{[低:{}]} ...等。 – ChristoD