2012-11-12 98 views
0

解析從控制器返回的json。出於某種原因,在返回字典時,我需要對關鍵元素執行「ToString()」操作,否則會出現錯誤。爲什麼?。樣本是否是正確的方式/序列化JSON的最佳方式?感謝MVC在Json中序列化對象,對象列表,集合

控制器:

// JSON 
    public ActionResult GizmosJsonObject() 
    { 
     var gizmo = new Gizmo 
     { 
      Id = 2343, 
      Name = "Some awesome name", 
      Quantity = 32, 
      IntroducedDate = DateTime.Now 
     }; 

     return this.Json(gizmo); 
    } 

    public ActionResult GizmosJsonObjectCollection() 
    { 
     var gizmos = new List<Gizmo>(); 
     gizmos.Add(new Gizmo 
     { 
      Id = 102, 
      Name = "some name1", 
      Quantity = 535, 
      IntroducedDate = DateTime.Now 
     }); 

     gizmos.Add(new Gizmo 
     { 
      Id = 122, 
      Name = "some name1", 
      Quantity = 135, 
      IntroducedDate = DateTime.Now 
     }); 

     gizmos.Add(new Gizmo 
     { 
      Id = 562, 
      Name = "some name1", 
      Quantity = 2, 
      IntroducedDate = DateTime.Now 
     }); 

     return this.Json(gizmos); 
    } 

    public ActionResult GizmosJsonListInts() 
    { 
     var gizmos = new List<int>(); 
     gizmos.Add(2); 
     gizmos.Add(56); 
     gizmos.Add(32); 

     return this.Json(gizmos); 
    } 

    public ActionResult GizmosJsonDictionaryInts() 
    { 
     var gizmos = new Dictionary<int, int>(); 
     gizmos.Add(23, 123); 
     gizmos.Add(26, 227); 
     gizmos.Add(54, 94323); 

     return this.Json(gizmos.ToDictionary(x => x.Key.ToString(), y => y.Value)); 
    } 

    public ActionResult GizmosJsonDictionaryStrings() 
    { 
     var gizmos = new Dictionary<string, string>(); 
     gizmos.Add("key1", "value1"); 
     gizmos.Add("Key2", "value2"); 
     gizmos.Add("key3", "value3"); 

     return this.Json(gizmos); 
    } 

查看:

<script type="text/javascript"> 
/*<![CDATA[*/ 
    $(function() { 

     // json object 
     $("a.Object").click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       url: '@Url.Action("GizmosJsonObject", "Home")', 
       contentType: 'application/json', 
       type: 'POST', 
       success: function (json) { 
        console.log(json.Id); 
        console.log(json.Name); 
        console.log(json.IntroducedDate); 

        // format date 
        var date = new Date(parseInt(json.IntroducedDate.substr(6))); 
        console.log(date); 
       } 
      }); 
     }); 

     // json object collection 
     $("a.ObjectCollection").click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       url: '@Url.Action("GizmosJsonObjectCollection", "Home")', 
       contentType: 'application/json', 
       type: 'POST', 
       success: function (json) { 
        $(json).each(function() { 
         console.log(this.Id); 
         console.log(this.Name); 
         console.log(this.IntroducedDate); 

         // format date 
         var date = new Date(parseInt(this.IntroducedDate.substr(6))); 
         console.log(date); 
        }); 
       } 
      }); 
     }); 

     // json list of ints 
     $("a.ListInts").click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       url: '@Url.Action("GizmosJsonListInts", "Home")', 
       contentType: 'application/json', 
       type: 'POST', 
       success: function (json) { 
        $(json).each(function (i, e) { 
         console.log(json[i]); 
        }); 
       } 
      }); 
     }); 

     // json dictionary of ints 
     $("a.DictionaryInts").click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       url: '@Url.Action("GizmosJsonDictionaryInts", "Home")', 
       contentType: 'application/json', 
       type: 'POST', 
       success: function (json) { 
        for (var key in json) { 
         if (json.hasOwnProperty(key)) { 
          var value = json[key]; 
          console.log(key); 
          console.log(value); 
         } 
        } 
       } 
      }); 
     }); 

     // json dictionary of strings 
     $("a.DictionaryStrings").click(function (e) { 
      e.preventDefault(); 
      $.ajax({ 
       url: '@Url.Action("GizmosJsonDictionaryStrings", "Home")', 
       contentType: 'application/json', 
       type: 'POST', 
       success: function (json) { 
        for (var key in json) { 
         if (json.hasOwnProperty(key)) { 
          var value = json[key]; 
          console.log(key); 
          console.log(value); 
         } 
        } 
       } 
      }); 
     }); 
    }); 
/*]]>*/ 
</script> 

回答

0

一個JSON關鍵的類型必須爲string的。請參閱右側邊欄http://www.json.org/,其中聲明一對必須採用string:value的形式。


只是爲了佐證,該文件在這裏http://www.ietf.org/rfc/rfc4627.txt狀態如下:

2.2。對象 對象結構表示爲圍繞零個或多個名稱/值對(或成員)的一對大括號 。名稱是 字符串。每個名稱後面都會出現一個冒號,將名稱分隔爲 。一個逗號將一個值與以下的名稱分開。對象內的名稱應該是唯一的。