C# 使用JSON.net将JSON解析为匿名对象[]
我有一个json字符串,我想将其解析为对象[]:C# 使用JSON.net将JSON解析为匿名对象[],c#,json,json.net,json-deserialization,C#,Json,Json.net,Json Deserialization,我有一个json字符串,我想将其解析为对象[]: { "Thing":"Thing","That":{"Item1":15,"Item2":"Moo","Item3":{"Count":27,"Type":"Frog"}}} 生成的匿名对象数组需要包含原始json对象的每个属性。我的问题是JsonConvert.DeserializeObject返回一种JContainer或JObject类型。我无法找到返回普通c#对象的方法 这是我以前尝试过的数组中当前的非功能代码。我不必使用JSON.ne
{ "Thing":"Thing","That":{"Item1":15,"Item2":"Moo","Item3":{"Count":27,"Type":"Frog"}}}
生成的匿名对象数组需要包含原始json对象的每个属性。我的问题是JsonConvert.DeserializeObject返回一种JContainer或JObject类型。我无法找到返回普通c#对象的方法
这是我以前尝试过的数组中当前的非功能代码。我不必使用JSON.net,但如果可能的话,我希望确保与生成JSON的代码兼容
JObject deserialized = JsonConvert.DeserializeObject<JObject>(dataString);
object[] data =
deserialized.Children().Where(x => x as JProperty != null).Select(x => x.Value<Object>()).ToArray();
JObject.Parse(jsonString.ToObject())
?string jsonString=“{”Thing”:“Thing”,“That”:{“Item1”:15,“Item2”:“Moo”,“Item3”:{“Count”:27,“Type”:“Frog”}
Object[]data=JsonConvert.DeserializeObject(jsonString);
?
您可以通过示例反序列化,使用以下匿名类型:
Type _executionType = typeof(CommandExecutionDummy);
CommandExecutionDummy provider = new CommandExecutionDummy();
var method = _executionType.GetMethod(model.Command,
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
if (method == null)
throw new InvalidCommandException(String.Format("Invalid Command - A command with a name of {0} could not be found", model.Command));
return method.Invoke(provider, model.CommandData);
string jsonString = "{name:\"me\",lastname:\"mylastname\"}";
var typeExample = new { name = "", lastname = "",data=new int[]{1,2,3} };
var result=JsonConvert.DeserializeAnonymousType(jsonString,typeExample);
int data1=result.data.Where(x => 1);
dynamic result2=JObject.Parse(jsonString);
另一种方式是在Json.Net中使用动态对象,如下所示:
Type _executionType = typeof(CommandExecutionDummy);
CommandExecutionDummy provider = new CommandExecutionDummy();
var method = _executionType.GetMethod(model.Command,
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
if (method == null)
throw new InvalidCommandException(String.Format("Invalid Command - A command with a name of {0} could not be found", model.Command));
return method.Invoke(provider, model.CommandData);
string jsonString = "{name:\"me\",lastname:\"mylastname\"}";
var typeExample = new { name = "", lastname = "",data=new int[]{1,2,3} };
var result=JsonConvert.DeserializeAnonymousType(jsonString,typeExample);
int data1=result.data.Where(x => 1);
dynamic result2=JObject.Parse(jsonString);
一个稍有不同的用例,其中JSON字符串是匿名类型的数组,下面的代码可以使用。本质上,它只是将匿名类型包装在一个数组中
string json = "[{\"Type\":\"text/xml\",\"Allowed\":\"true\"},{\"Type\":\"application/pdf\",\"Allowed\":\"true\"},{\"Type\":\"text/plain\",\"Allowed\":\"true\"}]";
JsonConvert.DeserializeAnonymousType(json, new[] { new { Type = "", Allowed = true } });
这将产生如下所示的结果
您想实现什么?运行时类型为
object
的对象将没有任何属性。JContainer
或JObject
或将其用作动态
,有什么问题吗?“动态”类型是完美的,如果我不想创建json的模板://objects from TFS REST API dynamic projects=JsonConvert.DeserializeObject(jsonProjects);var projectName=projects.value[0].name;