如何将特定的 Json 节点反序列化为字典<字符串,对象>

本文关键字:字典 字符串 对象 反序列化 节点 Json | 更新日期: 2023-09-27 17:56:04

我有一个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; }

}

我怎样才能做到这一点?

如何将特定的 Json 节点反序列化为字典<字符串,对象>

你真的非常接近。 您的类将按照您定义它们的方式正常工作,只是您需要将根类中的字典属性的名称从 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 = "{'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; }

            }