使用C#反序列化复杂的变量json [英] Deserialize complex, variable json with C#

查看:225
本文介绍了使用C#反序列化复杂的变量json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有变量Json并希望用Newtonsoft.Json反序列化。

这种类型的Json将无法转换为ac#class。该怎么办?

提前致谢。



variableKeyxxx123 - 密钥可以使用随机名称多次出现。

在某些时候,有更多价值。





Hi,

i have variable Json and want to deserialize with Newtonsoft.Json.
This type of Json will not be able to be transformed to a c# class. What to do?
Thanks in advance.

The "variableKeyxxx123" - keys can occur several times with random names.
In some times, there are "morevalues".


{
	"result": 
	{
		"computers": {
			"variableKeyxxx123": 
			{
				"id": 3333,
				"name": "name1",
				"xvalues": "xxValues"
			},
			"variableKeyxxx862": 
			{
				"id": 4444,
				"name": "name2",
				"xvalues": "xxValues"
			},
			"variableKeyxxx442": 
			{
				"id": 55555,
				"name": "name3",
				"xvalues": "xxValues",				
				"morevalues": {
					"valuexy": "hello"
				}
			}
		}
	}
}





我的尝试:



尝试了jsonToC#和Newtonsoft.Json但是从上面的Response创建类将导致错误,因为计算机在响应中命名。





What I have tried:

Tried jsonToC# and Newtonsoft.Json but creating class from the Response above will cause Errors because the Computers are named as in Response.

public class Computers
{
    public VariableKeyxxx123 variableKeyxxx123 { get; set; }
    public VariableKeyxxx862 variableKeyxxx862 { get; set; }
    public VariableKeyxxx442 variableKeyxxx442 { get; set; }
}

推荐答案

Newtonsoft支持ExpandoObject,专门用于支持架构无法定义的JSON因此是一个强类型的类。
Newtonsoft supports the ExpandoObject, specifically to support JSON that can't be defined by a "schema" and therefore a strongly typed class.


这篇关于使用C#反序列化复杂的变量json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆