如何制作json对象列表?

时间:2010-03-17 12:30:45

标签: javascript asp.net asp.net-mvc json

我正在使用asp.net mvc2并尝试从家庭控制器发送带有硬编码值的json对象列表,并在索引中接收它们.... 在下面的代码中,我发送1个json对象.... 我如何发送许多?

在家庭控制器中:

public ActionResult JsonValue()
        { 
            var result = new 
            { 
                pID = 1, 
                pName = "Lina",
                pStart = "",
                pEnd = "",
                pColor = "ff0000",
                pLink = "",
                pMile = 0,
                pRes = "Brian",
                pComp = 0,
                pGroup = 1,
                pParent = 0,
                pOpen = 1
            };


            return Json(result,JsonRequestBehavior.AllowGet);
        }

并在索引中接收它:

        var Jid = null;
        var Jname = null;
        var Jstart = null;
        var Jend = null;
        var Jcolor = null;
        var Jlink = null;
        var Jmile = null;
        var Jres = null;
        var Jcomp = null;
        var Jgroup = null;
        var Jparent = null;
        var Jopen = null;
        var Jtitle = null;
        var g = new JSGantt.GanttChart('g', document.getElementById('GanttChartDIV'), 'day');
        $(document).ready(function () {

            $.getJSON('../../Home/JsonValue', function (data) {
                Jid = data.pID;
                Jname = data.pName;
                Jstart = data.pStart;
                Jend = data.pEnd;
                Jcolor = data.pColor;
                Jlink = data.pLink;
                Jmile = data.pMile;
                Jres = data.pRes;
                Jcomp = data.pComp;
                Jgroup = data.pGroup;
                Jparent = data.pParent;
                Jopen = data.pOpen;
                Jtitle = '|id= ' + Jid + '|Name: ' + Jname + '|Start: ' + Jstart + '|End: ' + Jend;

            }); // end $.getJSON

提前感谢一百万... 丽娜

1 个答案:

答案 0 :(得分:1)

将它们添加到数组并返回(当然通过JSON调用)。

个人我会创建一个类而不是你拥有的匿名对象然后添加到一个通用列表,一旦你填充了列表,你可以将list.ToArray()传递给Json调用。我没有尝试,但你可以将列表直接传递给Json(我不确定它是否会从通用列表创建一个Json数组)。

编辑

看起来Json会将任何可枚举变成Json数组,所以我会说你可以传递列表泛型列表,我稍后会添加一些代码

下面添加了代码。

我不认为你应该亲自编写一个List<object>代码,因为我会为你的匿名对象创建一个类,所以你可以强力输入它。希望这会给你这个想法

public ActionResult JsonValue()
        {
            List<object> jsonlist = new List<object>();
            jsonlist.Add(new
            {
                pID = 1,
                pName = "Lina",
                pStart = "",
                pEnd = "",
                pColor = "ff0000",
                pLink = "",
                pMile = 0,
                pRes = "Brian",
                pComp = 0,
                pGroup = 1,
                pParent = 0,
                pOpen = 1
            });

            jsonlist.Add(new
            {
                pID = 1,
                pName = "Lina",
                pStart = "",
                pEnd = "",
                pColor = "ff0000",
                pLink = "",
                pMile = 0,
                pRes = "Brian",
                pComp = 0,
                pGroup = 1,
                pParent = 0,
                pOpen = 1
            });

            return Json(jsonlist,JsonRequestBehavior.AllowGet);
        }