2016-10-24 5 views
0

JSONの投稿データからモデルを探し出す際に問題があります。MVC5 Model Binding from 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>>();文を削除する必要がありますか?

ので、スキーム以下、最大ダウンから: - MetadataJSON

  1. 私は、彼らが常にあるだろう静的なデータを持っています - 。
  2. 私はMetadataJSONが値であるキーによる文字列のリストを持っています。
  3. このリストは、他のリストを含む配列(articulo)の要素です。
  4. 次に、値としてリストのキーと配列として文字列を持つ別のリストがあります。
  5. そして最後に、値を持つリストをキーとしてstringという別のリストがあります。

私のロジックの実装は問題ないと思うので、私はちょっと迷っています。間違いはおそらくJSONの世代です。

もちろん

、私のコントローラの方法:あなたが持っていない"http://www.xxxx.com/"という名前の属性を持つオブジェクトを送信している

Making the POST

VS 2015 debugging

答えて

0

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

応答それはあなたの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つのコメントを申し訳ありません。私はenterを押して、直接送信しました。 –

+0

多くのグループ(この場合私は "articulo"しか書いていませんでした)などを含むたくさんのURL(この場合は "xxx.com"のみを書きました)を持っています。 –

0

私は(私は、元のJSONでそれを解決する方法の他のアイデアを持っていない)のIDictionaryを使用して私の問題を「解決」と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を編集せずにそれを解決するために管理している場合、それは本当にいただければ幸いしてください。