【问题标题】:JSON.NET - outputting dynamic object dataJSON.NET - 输出动态对象数据
【发布时间】:2019-12-26 14:02:14
【问题描述】:

我正在开发一个项目,该项目采用提取的 JSON 文件并根据文件内容输出 UI。我的 JSON 如下所示:

{
    "template" : {
        "example" : {
            "name" : "Name",
            "wigglies" : [
                {
                    "displayname" : "Artifact Spots"
                }
            ],
            "othersrc" : [
                {
                    "displayname" : "Other Sources"
                }
            ],
            "reward" : "Donation Reward",
            "drops" : [
                {
                    "displayname" : "Monster Drops"
                }
            ],
            "price0base" : "Base Price",
            "edibility" : "Edibility",
            "iridium" : "",
            "image" : ""
        },
        "metadata" : {
            "author" : "Me",
            "version" : "1.0",
            "published" : "12/21/2019"
        }
    },
    "content" : {
        "categories" : [
            {
                "category" : "Artifacts",
                "subcategories" : [
                    {
                        "subcategory" : "Artifacts",
                        "items" : [
                            {
                                "name" : "Amphibian Fossil",
                                "wigglies" : [
                                    {
                                        "name" : "Forest"
                                    },
                                    {
                                        "name" : "Mountain"
                                    }
                                ],
                                "othersrc" : [
                                    {
                                        "name" : "Fishing Treasure Chest"
                                    }
                                ],
                                "price0base" : "150"
                            },
                            {
                                "name" : "Anchor",
                                "wigglies" : [
                                    {
                                        "name" : "The Beach"
                                    }
                                ],
                                "othersrc" : [
                                    {
                                        "name" : "Fishing Treasure Chest"
                                    },
                                    {
                                        "name" : "Artifact Trove"
                                    }
                                ],
                                "price0base" : "100"
                            }
                        ]
                    }
                ]
            },
            {
                "category" : "Equipment",
                "subcategories" : [
                    {
                        "subcategory" : "Refining",
                        "items" : [
                            {
                                "name" : "Charcoal Kiln"
                            },
                            {
                                "name" : "Crystalarium"
                            }
                        ]
                    },
                    {
                        "subcategory" : "Artisan",
                        "items" : [
                            {
                                "name" : "Bee House"
                            },
                            {
                                "name" : "Cask"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

我遇到的问题是如何以有用的方式返回此数据,同时允许 Content.Categories.Category.Subcategories.Subcategory.Items 包含的数组中的任意数据结构。我的示例 JSON 不是该数据的唯一结构;这只是一个例子。所以,那里也可能有更多的嵌套数组。

我知道我可以通过以下方式访问数据:

Content.Categories[0].Subcategories[0].Items[0]["name"]

虽然这会返回一个Json.Linq.Jtoken,但这并不是很有用。 Items 数组是 JObject[]。我觉得我在这里遗漏了一些明显的东西。

【问题讨论】:

    标签: c# json.net


    【解决方案1】:

    我相信您正在寻找 JToken 的字符串表示形式。您可以分别使用 .ToString().ToArray() 来获取名称和项目。

    
        JToken name = jsonFeed["content"]["categories"][0]["subcategories"][0]["items"][0]["name"];
        string nameString = jsonFeed["content"]["categories"][0]["subcategories"][0]["items"][0]["name"].ToString();
    
        JToken[] itemArray = jsonFeed["content"]["categories"][0]["subcategories"][0]["items"].ToArray();
    
    

    【讨论】:

    • 抱歉,我的问题不是很清楚 - 我如何在不明确引用属性的情况下枚举项目的内容?项目的属性是完全任意的。我的代码不知道它们。如何检索给定项目的键列表,并使用该列表获取值以在我的 UI 中作为 XAML 控件输出?
    • 您可以使用 Properties() 方法来获取对象的键。然后基于这些键,您可以遍历它。示例:newtonsoft.com/json/help/html/JObjectProperties.htm
    • 谢谢!!这让我朝着我需要去的方向前进。 :)
    【解决方案2】:

    你可以用这个:

    string jsonData = "";
    dynamic templates = JsonConvert.DeserializeObject<dynamic>(jsonData);
    var name = templates.example.Name;  // prints Name
    var categories = templates.Content.Categories; // list of categories
    
    // loop through the categories
    // or do whatever
    

    这样您的代码更易于阅读和管理。

    【讨论】:

    • 抱歉,我的问题不是很清楚 - 我如何在不明确引用属性的情况下枚举项目的内容?项目的属性是完全任意的。我的代码不知道它们。如何检索给定项目的键列表,并使用该列表获取值以在我的 UI 中作为 XAML 控件输出?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2015-04-20
    • 2011-08-22
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多