我在从JSON帖子数据中找出模型时遇到了麻烦。
JSON:
{
"http://www.xxxx.com/":{
"articulo":[
{
"descripcion":{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
},
"img":{
"innerHTML":"",
"nodeName":"IMG",
"treeDepth":17,
"className":"",
"childNodesLength":0,
"childrenLength":0,
"height":210,
"clientHeight":210,
"parentNodeName":"A",
"parentChildNodeslength":3
}
},
{
"comentarios":{
"innerHTML":"(52)",
"nodeName":"SPAN",
"treeDepth":20,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"DIV",
"parentChildNodeslength":15
}
}
]
}
}
我的模特:
public class GreatClass
{
public IList url { get; set; } = new List<KeyValuePair<string, IList>>();
private IList groups { get; set; } = new List<KeyValuePair<string, IList[]>>();
public IList[] subGroups { get; set; }
private IList metadata { get; set; } = new List<KeyValuePair<string, MetadataJSON>>();
public partial class MetadataJSON
{
public string innerHTML { get; set; }
public string nodeName { get; set; }
public int treeDepth { get; set; }
public string className { get; set; }
public int childNodesLength { get; set; }
public int childrenLength { get; set; }
public Nullable<int> height { get; set; }
public int clientHeight { get; set; }
public string parentNodeName { get; set; }
public int parentChildNodesLength { get; set; }
public string name { get; set; }
}
}
new List<KeyValuePair<string, IList>>();
new List<KeyValuePair<string, IList[]>>();
,= new List<KeyValuePair<string, MetadataJSON>>();
声明吗?因此,遵循该计划并从下到上:
我有点失落,因为我认为我的逻辑实现是正常的,错误可能是在JSON的生成。
当然,我的控制器方法:
[HttpPost]
public ActionResult GetJSONData(GreatClass JSONData)
{
if (ModelState.IsValid)
{
return Json(JSONData);
}
else
{
string errorMessage = "<div class=\"validation-summary-errors\">"
+ "The following errors occurred:<ul>";
foreach (var key in ModelState.Keys)
{
var error = ModelState[key].Errors.FirstOrDefault();
if (error != null)
{
errorMessage += "<li class=\"field-validation-error\">"
+ error.ErrorMessage + "</li>";
}
}
errorMessage += "</ul>";
return Json(errorMessage);
}
}
回应:
答案 0 :(得分:0)
您正在发送一个具有名为"http://www.xxxx.com/"
的属性的对象,您在C#模型中没有这个属性(并且您不能有这样的名称)。
您需要发送具有以下结构的对象:
{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
}
答案 1 :(得分:0)
我&#34;解决了#34;我使用IDictionary和修改JSON的问题(我对如何使用原始JSON解决它没有其他想法)。
public class GreatClass
{
public IDictionary<string, IDictionary<string, IDictionary<string, MetadataJSON>[]>> url { get; set; }
public partial class MetadataJSON
{
public string innerHTML { get; set; }
public string nodeName { get; set; }
public int treeDepth { get; set; }
public string className { get; set; }
public int childNodesLength { get; set; }
public int childrenLength { get; set; }
public Nullable<int> height { get; set; }
public int clientHeight { get; set; }
public string parentNodeName { get; set; }
public int parentChildNodesLength { get; set; }
public string name { get; set; }
}
}
和JSON:
{
"url":
[
{
Key: 'http://xxxx.com', Value:
[
{
Key: 'articulos', Value:
[
{
"descripcion":{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
},
"img":{
"innerHTML":"",
"nodeName":"IMG",
"treeDepth":17,
"className":"",
"childNodesLength":0,
"childrenLength":0,
"height":210,
"clientHeight":210,
"parentNodeName":"A",
"parentChildNodeslength":3
}
},
{
"comentarios":{
"innerHTML":"(52)",
"nodeName":"SPAN",
"treeDepth":20,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"DIV",
"parentChildNodeslength":15
}
}
]
}
]
}
]
}
如果你设法在没有编辑原始JSON的情况下解决它,那将非常感激。