我有一个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>
,其中字符串将是例如:"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; }
}
我怎样才能做到这一点?
你真的非常接近。 您的类将按照您定义它们的方式正常工作,只是您需要将根类中的字典属性的名称从 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
所以我无法以干净的方式做到这一点,但我相信这就是你要找的:
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; }
}
public class TestClass
{
private void Test()
{
var json = "{rn "637640274112277168": {rn "listingid": "637640274112277168",rn "price": 50,rn "fee": 7,rn "publisher_fee_app": 730,rn "publisher_fee_percent": "0.10000000149011612",rn "currencyid": "2005",rn "steam_fee": 2,rn "publisher_fee": 5,rn "converted_price": 1,rn "converted_fee": 2,rn "converted_currencyid": "2003",rn "converted_steam_fee": 1,rn "converted_publisher_fee": 1,rn "converted_price_per_unit": 1,rn "converted_fee_per_unit": 2,rn "converted_steam_fee_per_unit": 1,rn "converted_publisher_fee_per_unit": 1,rn "asset": {rn "currency": 0,rn "appid": 730,rn "contextid": "2",rn "id": "6542776191",rn "amount": "1"rn }rn },rn "194035710805671301": {rn "listingid": "194035710805671301",rn "price": 0,rn "fee": 0,rn "publisher_fee_app": 730,rn "publisher_fee_percent": "0.10000000149011612",rn "currencyid": "2001",rn "asset": {rn "currency": 0,rn "appid": 730,rn "contextid": "2",rn "id": "6825071309",rn "amount": "0"rn }rn }rn }rnrn";
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; }
}