簡體   English   中英

JSON的MVC5模型綁定

[英]MVC5 Model Binding from JSON

我在從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>>(); 陳述?

因此,遵循該方案並自下而上:

  1. 我有靜態數據,它們將一直存在-MetadataJSON-。
  2. 我有一個按字母順序排列的列表,其中MetadataJSON是值。
  3. 該列表是包含其他列表的數組(關節)的元素。
  4. 然后我有另一個列表,其中字符串作為鍵,列表數組作為值。
  5. 最后,我還有另一個列表,其中字符串作為鍵,包含列表作為值。

我有點迷茫,因為我認為我的邏輯實現還可以,而且錯誤可能出在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);
        }
    }

響應:

進行開機自檢

VS 2015調試

您正在發送一個對象,該對象的屬性名為"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
}

我使用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的情況下設法解決了該問題,將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM