2014-12-02 71 views
0
{"balances-and-info":{"on_hold":[],"available": {"USD":0.93033384},"usd_volume":"243.18","fee_bracket": {"maker":"0.00","taker":"0.60"},"global_usd_volume":"0.09942900"}} 

我有這樣的JSON響應,我試圖將其存儲在一個對象,但是你可以看到「平衡和-信息」不能被用作變量名。我一直在使用的方法是:使用JSON響應

RestClient client = new RestClient("http://currency-api.appspot.com/api/"); 
RestRequest request = new RestRequest(url); 

var response = client.Execute<Currency>(request); 

Currency obj = response.Data; 

凡顯然類是輕鬆了許多

public class Currency 
{ 
    public string rate { get; set; } 
} 

所以,我該怎麼處理呢?

回答

-1

與string.replace()在你的代碼

YourObject deserialized = parseResponse(obj.replace("balances-and-info", "balances_and_info")); 

YourObject parseResponse(string response) { 
    try 
    { 
     // https://www.nuget.org/packages/Newtonsoft.Json/ 
     // Json.NET 
     YourObject ret = JsonConvert.DeserializeObject<YourObject>(response); 
     return ret; 
    } 
    catch (JsonSerializationException) 
    { 
     // do something 
    } 
    return null; 
} 

YourObject

使用http://json2csharp.com/與balances_and_info

餘額-和信息,並生成你的對象(複製響應字符串,用balances_and_info替換balances-and-info並生成)

public class Available 
{ 
    public double USD { get; set; } 
} 

public class FeeBracket 
{ 
    public string maker { get; set; } 
    public string taker { get; set; } 
} 

public class BalancesAndInfo 
{ 
    public List<object> on_hold { get; set; } 
    public Available available { get; set; } 
    public string usd_volume { get; set; } 
    public FeeBracket fee_bracket { get; set; } 
    public string global_usd_volume { get; set; } 
} 

public class YourObject 
{ 
    public BalancesAndInfo balances_and_info { get; set; } 
} 
+0

不是正確的單詞,但你指出我在正確的方向。得到它的工作 - 謝謝你! – 2014-12-02 23:32:11

+0

我很高興它的幫助,仍然代碼錯過來自編碼直接進入瀏覽器在凌晨2點:) – SilentTremor 2014-12-03 07:50:19