JSON转换为C#类型 [英] Convert JSON to C# type
问题描述
我想从JS这样使用UKNOWN对象
i would like use uknown object from JS like this
{AA:{ABCD
1:{UIO:[]uio2:[]uio3:[opee1,opee2,opee3]}为abcd
2:空,ABCD 3:空,ABCD 4:空}}
{"aa":{"abcd 1":{"uio":[],"uio2":[],"uio3":["opee1","opee2","opee3"]},"abcd 2":null,"abcd 3":null,"abcd 4":null}}
送入MVC的contentType与应用/ JSON的。这个对象没有permament键,这样的名称为ABCD 1可以在未来的另一个名字。
sent into MVC with contentType 'application/json'. This object has no permament keys, like this name "abcd 1" can be another name in future.
我有功能测试(对象AA)和问题是,什么类型的我必须使用对象或字符串数组未知。谢谢
i have function Test(Object aa) and question is, what type i must use for unknown array of objects or strings. Thanks
推荐答案
终于让我找到解决办法。使用Newtonsoft.Json这code作为样本进行动态数据结构:
Finally I found solution. Use Newtonsoft.Json and this code as sample for dynamic data structure:
$.ajax({
...
data: {data: JSON.stringify({first: "name", next: ["a", "b"], last: {o: "in"}})}
})
[HttpPost]
public JsonResult SaveMenu(String data)
{
dynamic JData = JObject.Parse(data);
//--now JData.first == "name"
if (JData.last is JObject)
{
}
//--or dynamic foreach
foreach (dynamic own in JData)
{
//--own.Name == first, next and last
//--access to variable == JData[own.Name]
if (JData[own.Name] is JArray)
{
foreach (String var in JData[own.Name])
{
}
}
}
}
这篇关于JSON转换为C#类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!