2016-10-24 61 views
0

我在從JSON發佈數據中找出模型時遇到了麻煩。來自JSON的MVC5模型綁定

的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); 
     } 
    } 

響應:

Making the POST

VS 2015 debugging

回答

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 
} 
+0

這不會是我想要的結構。 –

+0

因爲您將完整的元數據對象直接分配給url。其餘的呢?組和子組? 對不起2條評論,我按了回車,直接發送。 –

+0

我想有很多網址(在這種情況下,我只寫了「xxx.com」),其中包含很多組(在這種情況下,我只寫了「articulo」)等。 –

0

我「解決」使用的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,這將非常感激。