【问题标题】:Serialize JSON String To Dynamic Object将 JSON 字符串序列化为动态对象
【发布时间】:2015-08-24 16:49:00
【问题描述】:

好的,如果我的术语有误,请原谅我。我正在尝试执行以下操作。

  1. 从 URL 获取 JSON 字符串
  2. 将所述字符串解析为对象。

通常对我来说这很容易。通常 JSON 对象是静态名称。

问题出在子json对象因人而异。如果您更改库存,它们也会发生变化。

现在,我需要能够将它放入一个对象中,并对其进行查询(不一定是 linq,而是随意提取数据)。有没有人有任何信息?我尝试创建一个动态对象(如此处所示Deserialize JSON into C# dynamic object?),但它对我不起作用。

该链接真的适合我的目的吗?我只是错误地实现了它吗?

以下是 JSON 的转储:http://chopapp.com/#ro46jfay

感谢您的所有帮助。

【问题讨论】:

  • 你指向一个有很多答案的问题,然后说“它对我不起作用”,但不要显示你尝试过的任何代码。

标签: c# json


【解决方案1】:

Newtonsoft Json.net 支持从 json 创建动态对象。

var obj = JsonConvert.DeserializeObject<dynamic>(json); 

【讨论】:

  • 对,但我无法成功访问它的层次结构并获取数据。 code string name = item["rgDescriptions[0]"]; string name = item["rgDescriptions"];
  • item["rgDescriptions[0]"]; - 这没有意义。正确的方式类似于obj["rgDescriptions"][0]["name"]。想象它像“字符串 - > 对象”的字典,并像字典而不是一些神话般的 json 一样导航它们。
  • 但这并没有让我迭代。请查看示例 JSON。层次结构如下:ROOT -> rgDescriptios -> RandomName 如何访问所有随机名称及其子项?
【解决方案2】:

事实上,您的数据并不像您想象的那样动态。所有这些用作属性名称的 ID 都可以反序列化为 Dictionary&lt;string,SomeObject&gt;

虽然你的 json 比 this question 中的更复杂,但同样的想法可以很容易地使用。

所以你的模型可以如下:

public class RGInventory
{
    public string id { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string amount { get; set; }
    public int pos { get; set; }
}

public class AppData
{
    public string def_index { get; set; }
    public int is_itemset_name { get; set; }
}

public class Description
{
    public string type { get; set; }
    public string value { get; set; }
    public string color { get; set; }
    public AppData app_data { get; set; }
}

public class Action
{
    public string name { get; set; }
    public string link { get; set; }
}

public class MarketAction
{
    public string name { get; set; }
    public string link { get; set; }
}

public class Tag
{
    public string internal_name { get; set; }
    public string name { get; set; }
    public string category { get; set; }
    public string category_name { get; set; }
    public string color { get; set; }
}

public class RGDescription
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string icon_url { get; set; }
    public string icon_url_large { get; set; }
    public string icon_drag_url { get; set; }
    public string name { get; set; }
    public string market_hash_name { get; set; }
    public string market_name { get; set; }
    public string name_color { get; set; }
    public string background_color { get; set; }
    public string type { get; set; }
    public int tradable { get; set; }
    public int marketable { get; set; }
    public int commodity { get; set; }
    public string market_tradable_restriction { get; set; }
    public List<Description> descriptions { get; set; }
    public List<Action> actions { get; set; }
    public List<MarketAction> market_actions { get; set; }
    public List<Tag> tags { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public bool more { get; set; }
    public bool more_start { get; set; }
    public Dictionary<string, RGInventory> rgInventory { get; set; }
    public Dictionary<string, RGDescription> rgDescriptions { get; set; }
}

现在您可以将(使用Json.Net)反序列化为

var obj = JsonConvert.DeserializeObject<RootObject>(json);

层次结构如下: ROOT -> rgDescriptios -> RandomName 如何访问所有随机名称及其子名称?

一个示例 linq 可以写成

var appids = obj.rgDescriptions.Select(x => x.Value.appid).ToList();

以类型安全的方式。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 2020-02-03
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多