c# - 如何将特定的 Json 节点反序列化为 Dictionary<string,object>?

标签 c# json parsing json.net

我有一个 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)看起来像这样:

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 以反射(reflect) 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);

fiddle :https://dotnetfiddle.net/V8LsXY

关于c# - 如何将特定的 Json 节点反序列化为 Dictionary<string,object>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266393/

相关文章:

ios - URL 作为 Xcode 中的字符串和值

c# - 如何解析日期时间秒.ms

c# - AppFabric DataCache 按键获取值?

c# - .NET 可移植框架中的 System.Drawing.Bitmap

对用户隐藏的 C# 设置

java - 解析 JSON 抛出异常

python - 在 Django 中使用请求解析 JSON

javascript - 如何在asp.net MVC中获取外部JavaScript文件中的 session 值、临时数据?

Javascript 使用变量作为键从 json 中获取值

c# - 更改整数的默认 NumberStyles?