[英]How to deserialize specific Json node to Dictionary<string,object>?
我有一個JSON響應,如下所示:
{
"success": true,
"more": false,
"results_html": "a lot of html in here",
"listinginfo": {
"637640274112277168": {
"listingid": "637640274112277168",
"price": 50,
"fee": 7,
"publisher_fee_app": 730,
"publisher_fee_percent": "0.10000000149011612",
"currencyid": "2005",
"steam_fee": 2,
"publisher_fee": 5,
"converted_price": 1,
"converted_fee": 2,
"converted_currencyid": "2003",
"converted_steam_fee": 1,
"converted_publisher_fee": 1,
"converted_price_per_unit": 1,
"converted_fee_per_unit": 2,
"converted_steam_fee_per_unit": 1,
"converted_publisher_fee_per_unit": 1,
"asset": {
"currency": 0,
"appid": 730,
"contextid": "2",
"id": "6542776191",
"amount": "1"
}
},
"194035710805671301": {
"listingid": "194035710805671301",
"price": 0,
"fee": 0,
"publisher_fee_app": 730,
"publisher_fee_percent": "0.10000000149011612",
"currencyid": "2001",
"asset": {
"currency": 0,
"appid": 730,
"contextid": "2",
"id": "6825071309",
"amount": "0"
}
},
}//end listinginfo
//more fields here..
}
我需要從“listinginfo”節點獲取信息並將它們添加到dictionary<string, NewListedItem>
,其中string將是例如:“637640274112277168”,列表將是“NewListedItem”類型,包含所有信息從那個子節點。 我的課程看起來像這樣:
class listinginfo
{
public Dictionary<string, NewListedItem> Items;
}
class Asset
{
public string currency { get; set; }
public string appid { get; set; }
public string contextid { get; set; }
public string id { get; set; }
public string amount { get; set; }
}
class NewListedItem
{
public string listingid { get; set; }
public string price { get; set; }
public string fee { get; set; }
public string publisher_fee_app { get; set; }
public string publisher_fee_percent { get; set; }
public string steam_fee { get; set; }
public string publisher_fee { get; set; }
public string converted_price { get; set; }
public string converted_fee { get; set; }
public string converted_currencyid { get; set; }
public string converted_steam_fee { get; set; }
public string converted_publisher_fee { get; set; }
public string converted_fee_per_unit { get; set; }
public string converted_steam_fee_per_unit { get; set; }
public string converted_publisher_fee_per_unit { get; set; }
public Asset asset { get; set; }
}
我怎樣才能實現這一目標?
所以我無法以干凈的方式做到這一點,但我相信這正是你要找的:
var jsonString = @"{
'success': true,
'more': false,
'results_html': 'a lot of html in here',
'listinginfo': {
'637640274112277168': {
'listingid': '637640274112277168',
'price': 50,
'fee': 7,
'publisher_fee_app': 730,
'publisher_fee_percent': '0.10000000149011612',
'currencyid': '2005',
'steam_fee': 2,
'publisher_fee': 5,
'converted_price': 1,
'converted_fee': 2,
'converted_currencyid': '2003',
'converted_steam_fee': 1,
'converted_publisher_fee': 1,
'converted_price_per_unit': 1,
'converted_fee_per_unit': 2,
'converted_steam_fee_per_unit': 1,
'converted_publisher_fee_per_unit': 1,
'asset': {
'currency': 0,
'appid': 730,
'contextid': '2',
'id': '6542776191',
'amount': '1'
}
},
'194035710805671301': {
'listingid': '194035710805671301',
'price': 0,
'fee': 0,
'publisher_fee_app': 730,
'publisher_fee_percent': '0.10000000149011612',
'currencyid': '2001',
'asset': {
'currency': 0,
'appid': 730,
'contextid': '2',
'id': '6825071309',
'amount': '0'
}
},
}
}";
var json = JObject.Parse(jsonString);
List<JToken> results = json["listinginfo"].Children().Children().ToList();
var dictionaryResults = new ListingInfo();
foreach (var token in results)
{
var info = JsonConvert.DeserializeObject<NewListedItem>(token.ToString());
List<NewListedItem> listInfo = new List<NewListedItem>();
listInfo.Add(info);
dictionaryResults.Items = new Dictionary<string, List<NewListedItem>>();
dictionaryResults.Items.Add(info.Listingid, listInfo);
}
我還修改了你的類的屬性名稱:
public class Asset
{
public string Currency { get; set; }
public string Appid { get; set; }
public string Contextid { get; set; }
public string Id { get; set; }
public string Amount { get; set; }
}
public class NewListedItem
{
public string Listingid { get; set; }
public string Price { get; set; }
public string Fee { get; set; }
[JsonProperty("publisher_fee_app")]
public string PublisherFeeApp { get; set; }
[JsonProperty("publisher_fee_percent")]
public string PublisherFeePercent { get; set; }
[JsonProperty("steam_fee")]
public string SteamFee { get; set; }
[JsonProperty("publisher_fee")]
public string PublisherFee { get; set; }
[JsonProperty("converted_price")]
public string ConvertedPrice { get; set; }
[JsonProperty("converted_fee")]
public string ConvertedFee { get; set; }
[JsonProperty("converted_currencyid")]
public string ConvertedCurrencyid { get; set; }
[JsonProperty("converted_steam_fee")]
public string ConvertedSteamFee { get; set; }
[JsonProperty("converted_publisher_fee")]
public string ConvertedPublisherFee { get; set; }
[JsonProperty("converted_fee_per_unit")]
public string ConvertedFeePerUnit { get; set; }
[JsonProperty("converted_steam_fee_per_unit")]
public string ConvertedSteamFeePerUnit { get; set; }
[JsonProperty("converted_publisher_fee_per_unit")]
public string ConvertedPublisherFeePerUnit { get; set; }
public Asset Asset { get; set; }
}
你真的很親密。 除了需要將根類中的字典屬性的名稱從Items
更改為listinginfo
以鏡像JSON屬性名稱之外,您的類將在您定義它們時正常工作。 (或者,您可以使用[JsonProperty]
屬性將Items
字典映射到JSON中的listinginfo
屬性。)進行更改后,我還建議將您的根類從listinginfo
重命名為對您有意義的其他內容,例如也許是ListingResponse
,但這不是絕對必要的。
完成這些更改后,您的根類應如下所示:
public class ListingResponse
{
public Dictionary<string, NewListedItem> listinginfo { get; set; }
}
或者,如果您更喜歡屬性方法:
public class ListingResponse
{
[JsonProperty("listinginfo")]
public Dictionary<string, NewListedItem> Items { get; set; }
}
然后,您應該能夠將JSON反序列化到此類中,並且您的字典將按預期填充:
ListingResponse response = JsonConvert.DeserializeObject<ListingResponse>(json);
小提琴: https : //dotnetfiddle.net/V8LsXY
public class TestClass
{
private void Test()
{
var json = "{\r\n \"637640274112277168\": {\r\n \"listingid\": \"637640274112277168\",\r\n \"price\": 50,\r\n \"fee\": 7,\r\n \"publisher_fee_app\": 730,\r\n \"publisher_fee_percent\": \"0.10000000149011612\",\r\n \"currencyid\": \"2005\",\r\n \"steam_fee\": 2,\r\n \"publisher_fee\": 5,\r\n \"converted_price\": 1,\r\n \"converted_fee\": 2,\r\n \"converted_currencyid\": \"2003\",\r\n \"converted_steam_fee\": 1,\r\n \"converted_publisher_fee\": 1,\r\n \"converted_price_per_unit\": 1,\r\n \"converted_fee_per_unit\": 2,\r\n \"converted_steam_fee_per_unit\": 1,\r\n \"converted_publisher_fee_per_unit\": 1,\r\n \"asset\": {\r\n \"currency\": 0,\r\n \"appid\": 730,\r\n \"contextid\": \"2\",\r\n \"id\": \"6542776191\",\r\n \"amount\": \"1\"\r\n }\r\n },\r\n \"194035710805671301\": {\r\n \"listingid\": \"194035710805671301\",\r\n \"price\": 0,\r\n \"fee\": 0,\r\n \"publisher_fee_app\": 730,\r\n \"publisher_fee_percent\": \"0.10000000149011612\",\r\n \"currencyid\": \"2001\",\r\n \"asset\": {\r\n \"currency\": 0,\r\n \"appid\": 730,\r\n \"contextid\": \"2\",\r\n \"id\": \"6825071309\",\r\n \"amount\": \"0\"\r\n }\r\n }\r\n }\r\n\r\n";
Dictionary<string, NewListedItem> values = JsonConvert.DeserializeObject<Dictionary<string, NewListedItem>>(json);
}
}
class listinginfo
{
public Dictionary<string, List<NewListedItem>> Items;
}
class Asset
{
public string currency { get; set; }
public string appid { get; set; }
public string contextid { get; set; }
public string id { get; set; }
public string amount { get; set; }
}
class NewListedItem
{
public string listingid { get; set; }
public string price { get; set; }
public string fee { get; set; }
public string publisher_fee_app { get; set; }
public string publisher_fee_percent { get; set; }
public string steam_fee { get; set; }
public string publisher_fee { get; set; }
public string converted_price { get; set; }
public string converted_fee { get; set; }
public string converted_currencyid { get; set; }
public string converted_steam_fee { get; set; }
public string converted_publisher_fee { get; set; }
public string converted_fee_per_unit { get; set; }
public string converted_steam_fee_per_unit { get; set; }
public string converted_publisher_fee_per_unit { get; set; }
public Asset asset { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.