Asp.net mvc MVC序列化Json中的对象、对象列表和集合
解析从控制器返回的json。出于某种原因,在返回字典时,我需要对key元素执行“ToString()”,否则会出现错误。为什么?下面的示例是否是序列化json的正确方法/最佳方法?谢谢 控制器:Asp.net mvc MVC序列化Json中的对象、对象列表和集合,asp.net-mvc,json,asp.net-mvc-3,jquery,getjson,Asp.net Mvc,Json,Asp.net Mvc 3,Jquery,Getjson,解析从控制器返回的json。出于某种原因,在返回字典时,我需要对key元素执行“ToString()”,否则会出现错误。为什么?下面的示例是否是序列化json的正确方法/最佳方法?谢谢 控制器: // JSON public ActionResult GizmosJsonObject() { var gizmo = new Gizmo { Id = 2343, Name = "Some awe
// 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);
}
//JSON
public ActionResult GizmosJsonObject()的
{
var gizmo=新gizmo
{
Id=2343,
Name=“一些很棒的名字”,
数量=32,
introducteddate=DateTime.Now
};
返回this.Json(gizmo);
}
public ActionResult GizmosJsonObject集合()
{
var gizmos=新列表();
小控件。添加(新小控件)
{
Id=102,
Name=“some name1”,
数量=535,
introducteddate=DateTime.Now
});
小控件。添加(新小控件)
{
Id=122,
Name=“some name1”,
数量=135,
introducteddate=DateTime.Now
});
小控件。添加(新小控件)
{
Id=562,
Name=“some name1”,
数量=2,
introducteddate=DateTime.Now
});
返回this.Json(gizmos);
}
public ActionResult GizMosJSonListings()
{
var gizmos=新列表();
小发明。添加(2);
小发明.添加(56);
小发明。添加(32);
返回this.Json(gizmos);
}
公共行动结果GizmosJSONDICTIONAINTS()
{
var gizmos=新字典();
gizmos.Add(23123);
gizmos.Add(26227);
gizmos.Add(5494323);
返回this.Json(gizmos.ToDictionary(x=>x.Key.ToString(),y=>y.Value));
}
公共行动结果GizmosJsonDictionaryStrings()
{
var gizmos=新字典();
添加(“键1”、“值1”);
添加(“键2”、“值2”);
添加(“键3”、“值3”);
返回this.Json(gizmos);
}
视图:
/**/
JSON密钥的类型必须为字符串
。请参见右侧的边栏,其中指出一对必须采用string:value的形式
仅为佐证,本文件如下所述: 2.2. 物体 对象结构表示为一对花括号 围绕零个或多个名称/值对(或成员)。一个名字就是一个名字 一串每个名称后面都有一个冒号,用于分隔名称 从价值上来说。一个逗号将一个值与后面的值分隔开 名称对象中的名称应该是唯一的。
<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>