C# 将JSON字符串序列化为动态对象
好的,如果我把术语弄错了,请原谅。我正在尝试做以下工作C# 将JSON字符串序列化为动态对象,c#,json,C#,Json,好的,如果我把术语弄错了,请原谅。我正在尝试做以下工作 从URL获取JSON字符串 将所述字符串解析为对象 通常对我来说这很容易。通常JSON对象是静态名称 问题在于子json对象因人而异。如果你改变库存,它们也会改变 现在,我需要能够将其放入一个对象中,并查询它(不一定是linq,但可以随意提取数据)。有人有什么消息吗?我试图创建一个动态对象(如图所示),但它不适合我 这个链接真的适合我的目的吗?我只是没有正确地实现它吗 下面是JSON的转储: 感谢您的帮助。支持从json创建动态对象 var
var obj = JsonConvert.DeserializeObject<dynamic>(json);
var obj=JsonConvert.DeserializeObject(json);
事实上,您的数据并不像您想象的那样动态。所有这些用作属性名称的ID都可以反序列化到字典
虽然您的json比中的更复杂,但同样的想法也可以很容易地使用
因此,您的模型可以如下所示:
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; }
}
以一种类型安全的方式。是的,但我未能成功访问它的继承权并获取数据<代码>代码字符串名称=项[“rgDescriptions[0]”;字符串名称=项[“rgDescriptions”]代码>
项[“rgDescriptions[0]”代码>-这没有意义。正确的方式类似于obj[“rgDescriptions”][0][“name”]
。想象一下,它就像“字符串->对象”的字典一样,并像字典一样导航,而不是像神话中的json一样导航。但这并不能让我反复浏览。请看一下示例JSON。层次结构如下:ROOT->rgDescriptios->RandomName我如何访问所有随机名称及其子项?你指向一个有大量答案的问题,然后说“它对我不起作用”,但不显示任何你尝试过的代码。@DevinStokes尽管我不理解你的沉默,下面是我的另一个答案,看起来您使用了VS->Edit->PasteSpecial->Paste JSON to Class:)
var obj = JsonConvert.DeserializeObject<RootObject>(json);
var appids = obj.rgDescriptions.Select(x => x.Value.appid).ToList();