C# 使用 JSON.net 将 JSON 解析为匿名对象 []
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19918049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse JSON into anonymous object[] using JSON.net
提问by Ritz
I have a json string that I want to parse into an object[]:
我有一个 json 字符串,我想将其解析为一个对象 []:
{ "Thing":"Thing","That":{"Item1":15,"Item2":"Moo","Item3":{"Count":27,"Type":"Frog"}}}
The resulting anonymous object array needs to contain each of the properties of the original json object. My issue is that JsonConvert.DeserializeObject returns a type of JContainer or JObject. I have not been able to identify a way to return a plain vanilla c# object.
生成的匿名对象数组需要包含原始 json 对象的每个属性。我的问题是 JsonConvert.DeserializeObject 返回 JContainer 或 JObject 的类型。我无法确定返回普通 c# 对象的方法。
This is my current non-functional code from an array of previous attempts. I do not have to use JSON.net but I would like to if possible to ensure compatibility wiith the code generating the json.
这是我当前的非功能性代码,来自之前的一系列尝试。我不必使用 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();
Update
更新
I am using the produced object array to invoke methods via reflection. The types of the parsed json objects are not known at runtime. The problem sticking point is that JObject or JContainer object types do not match the signatures of the methods being invoked. Dynamic has this same side-effect. Methods are being invoked like this:
我正在使用生成的对象数组通过反射调用方法。解析的 json 对象的类型在运行时是未知的。问题的症结在于 JObject 或 JContainer 对象类型与被调用方法的签名不匹配。Dynamic 也有同样的副作用。方法被这样调用:
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);
采纳答案by majimenezp
you can deserialize by example, using an anonymous type like this:
您可以通过示例反序列化,使用如下匿名类型:
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);
Other way in Json.Net it's using a dynamic object like this:
Json.Net 中的另一种方式是使用这样的动态对象:
dynamic result2=JObject.Parse(jsonString);
回答by Shrike
JObject.Parse(jsonString).ToObject<MyType>()
?
?
回答by SJP
string jsonString = "{ "Thing":"Thing","That":{"Item1":15,"Item2":"Moo","Item3":{"Count":27,"Type":"Frog"}}}"
Object[] data = JsonConvert.DeserializeObject<Object>(jsonString);
?
回答by Tedford
A slightly different use case in which the JSON string is an array of anonymous types the following will work. Essentially it just wraps the anonymous types within an array.
一个稍微不同的用例,其中 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 } });
This results in the following as visualized by Linqpad.
这会导致以下结果,如Linqpad 所示。