[英]Handle dynamic structure of Json
I have Json structure like this : 我有这样的Json结构:
metadata : {
id : "something",
data : {
1 : {
id : "something",
simple : {
ASD3472GJVMKG : { id : "something", name : "something" },
A892SADKLAWEN : { .. },
KVMSD309234KG : { .. },
2 : { .. },
...
There are two attributes which generated dynamically , they are attributes of data
and attributes of simple
. 动态生成有两个属性,它们是
data
属性和simple
属性。 The name attributes of data
are kind of number which generated incrementaly. data
的名称属性是递增生成的数字种类。 The name attributes of simple
are random string which generated dynamically. simple
的名称属性是动态生成的随机字符串。
Actually, I have done deserializing the attributes of simple
with kind of process that not simple (foreach and substring everywhere). 实际上,我已经完成了对
simple
属性的反序列化,这种过程并不简单(foreach和substring无处不在)。 BTW, I am using Json.NET and C#. 顺便说一下,我正在使用Json.NET和C#。 What kind of solution I need to do with this bad Json structure?
我需要用这种糟糕的Json结构做什么样的解决方案? I have seen JObject and Dictionary class but still don't get it to figure that.
我已经看过JObject和Dictionary类,但仍然没有弄明白。
Thanks in advance 提前致谢
The structure you have shown above is not valid JSON. 您在上面显示的结构不是有效的JSON。 You'll need to either produce valid JSON for input to JSON.NET, or create a special parser especially for this type of data.
您需要为JSON.NET输入生成有效的JSON,或者为此类数据创建特殊的解析器。
If you are working with valid JSON and simply don't know the property names in advance, you can use one of the following deserialization methods. 如果您正在使用有效的JSON并且事先不知道属性名称,则可以使用以下反序列化方法之一。
JObject
instead of a more specific type. JObject
而不是更具体的类型。 This allows you to access the individual properties of the object. Dictionary<string, JObject>
. Dictionary<string, JObject>
。 In practice this is nearly equivalent to the previous option. Deserialize the object to an object that contains the following field. 将对象反序列化为包含以下字段的对象。 During deserialization, all properties which do not map to some other field or property in the object model will be added to this dictionary.
在反序列化期间,所有未映射到对象模型中的某个其他字段或属性的属性都将添加到此字典中。
[JsonExtensionData] private Dictionary<string, JObject> _extensionData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.