Creating a JSON array in C#

前端 未结 4 2015
北恋
北恋 2021-01-30 02:04

Ok, so I am trying to send POST commands over an http connection, and using JSON formatting to do so. I am writing the program to do this in C#, and was wondering how I would f

4条回答
  •  你的背包
    2021-01-30 02:48

    Also , with Anonymous types ( I prefer not to do this) -- this is just another approach.

    void Main()
    {
        var x = new
        {
            items = new[]
            {
                new
                {
                    name = "command", index = "X", optional = "0"
                },
                new
                {
                    name = "command", index = "X", optional = "0"
                }
            }
        };
        JavaScriptSerializer js = new JavaScriptSerializer(); //system.web.extension assembly....
        Console.WriteLine(js.Serialize(x));
    }
    

    result :

    {"items":[{"name":"command","index":"X","optional":"0"},{"name":"command","index":"X","optional":"0"}]}

提交回复
热议问题