我有以下Json:
{
"BTC_BCN": {
"id": 7,
"last": "0.00000091",
"lowestAsk": "0.00000091",
"highestBid": "0.00000090",
"percentChange": "-0.16513761",
"baseVolume": "7448.24465884",
"quoteVolume": "8447512040.11460590",
"isFrozen": "0",
"high24hr": "0.00000115",
"low24hr": "0.00000071"
},
"BTC_BELA": {
"id": 8,
"last": "0.00009098",
"lowestAsk": "0.00009178",
"highestBid": "0.00009101",
"percentChange": "0.02918552",
"baseVolume": "143.21212880",
"quoteVolume": "1621911.96303103",
"isFrozen": "0",
"high24hr": "0.00009196",
"low24hr": "0.00008681"
},
"BTC_BLK": {
"id": 10,
"last": "0.00010199",
"lowestAsk": "0.00010262",
"highestBid": "0.00010204",
"percentChange": "0.03859470",
"baseVolume": "51.34937066",
"quoteVolume": "512481.76267599",
"isFrozen": "0",
"high24hr": "0.00010752",
"low24hr": "0.00009375"
}
}
我想创建如下结构:
public class QuoteDetail
{
public int id { get; set; }
public string last { get; set; }
public string lowestAsk { get; set; }
public string highestBid { get; set; }
public string percentChange { get; set; }
public string baseVolume { get; set; }
public string quoteVolume { get; set; }
public string isFrozen { get; set; }
public string high24hr { get; set; }
public string low24hr { get; set; }
}
public class Quote
{
public string SymbolName {get; set;}
public QuoteDetail QuoteDetail {get; set;}
}
我想要的是一个符号名称包含BTC_BCN的报价列表,以及包含各个字段的报价详细信息。
我唯一可以开始工作的是为每个符号名称创建单独的类....而不是我需要的。
我正在使用RestSharp。
任何想法都将不胜感激!
答案 0 :(得分:3)
鉴于JSON的当前结构,首先将其转换为字典以获取SymbolName
作为关键字。从那里简单地在字典上执行linq投影,将key
作为SymbolName
,value
作为QuoteDetails
。
var dictionary = JsonConvert.DeserializeObject<IDictionary<string, QuoteDetail>>(json);
var quotes = dictionary
.Select(kvp => new Quote { SymbolName = kvp.Key, QuoteDetail = kvp.Value })
.ToList();
答案 1 :(得分:0)
你几乎完成了所有事情。只需创建以下类:
setWifiApEnabled
使用Newtonsoft.Json nuget包执行以下操作:
public class MyClass
{
public QuoteDetail BTC_BCN { get; set; }
public QuoteDetail BTC_BELA { get; set; }
public QuoteDetail BTC_BLK { get; set; }
}
答案 2 :(得分:0)
在http://json2csharp.com的帮助下,从JSON创建类结构。由于您对所有类(如BTC_BCN,BTC_BELA,BTC_BLK)具有相同的结构,因此只使用具有通用名称的单个类,并创建具有所需名称的属性,例如
public class ObjectData
{
public int id { get; set; }
public string last { get; set; }
public string lowestAsk { get; set; }
public string highestBid { get; set; }
public string percentChange { get; set; }
public string baseVolume { get; set; }
public string quoteVolume { get; set; }
public string isFrozen { get; set; }
public string high24hr { get; set; }
public string low24hr { get; set; }
}
public class RootObject
{
public ObjectData BTC_BCN { get; set; }
public ObjectData BTC_BELA { get; set; }
public ObjectData BTC_BLK { get; set; }
}
比反序列化你的对象 RootObject rootObject = JsonConvert.DeserializeObject(jsonData);