70

好的,所以我正在尝试通过 http 连接发送 POST 命令,并使用 JSON 格式来执行此操作。我正在用 C# 编写程序来执行此操作,并且想知道如何格式化要作为 JSON 传递给服务器的值数组。

目前我有这个:

new {name = "command" , index = "X", optional = "0"}

在 JSON 中转换为:

"name": "command",
"index": "X",
"optional": "0"

我想创建一个名为 items 的数组,其中每个元素都包含这三个值。所以它本质上是一个对象数组,其中对象包含一个名称、一个索引和一个可选字段。

我的猜测是,它会是这样的:

new {items = [(name = "command" , index = "X", optional = "0"), 
              (name = "status" , index = "X", optional = "0")]}

如果它是正确的语法,它将在 JSON 中转换为:

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

但是,显然我做错了。想法?任何帮助表示赞赏。

4

4 回答 4

106

你很近。这应该可以解决问题:

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

如果您的来源是某种可枚举的,您可能想要这样做:

new {items = source.Select(item => new 
{
    name = item.Name, index = item.Index, options = item.Optional
})};
于 2013-06-03T14:11:37.807 回答
44

您最好为每个项目创建一些类,而不是使用匿名对象。在你正在序列化的对象中,你应该有这些项目的数组。例如:

public class Item
{
    public string name { get; set; }
    public string index { get; set; }
    public string optional { get; set; }
}

public class RootObject
{
    public List<Item> items { get; set; }
}

用法:

var objectToSerialize = new RootObject();
objectToSerialize.items = new List<Item> 
                          {
                             new Item { name = "test1", index = "index1" },
                             new Item { name = "test2", index = "index2" }
                          };

结果,如果您需要更改数据结构,则不必多次更改。

ps这是一个非常好的复杂json的工具

于 2013-06-03T14:14:09.830 回答
12

此外,对于匿名类型(我喜欢这样做)——这只是另一种方法。

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

结果 :

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

于 2013-06-03T14:18:29.283 回答
-6
new {var_data[counter] =new [] { 
                new{  "S NO":  "+ obj_Data_Row["F_ID_ITEM_MASTER"].ToString() +","PART NAME": " + obj_Data_Row["F_PART_NAME"].ToString() + ","PART ID": " + obj_Data_Row["F_PART_ID"].ToString() + ","PART CODE":" + obj_Data_Row["F_PART_CODE"].ToString() + ", "CIENT PART ID": " + obj_Data_Row["F_ID_CLIENT"].ToString() + ","TYPES":" + obj_Data_Row["F_TYPE"].ToString() + ","UOM":" + obj_Data_Row["F_UOM"].ToString() + ","SPECIFICATION":" + obj_Data_Row["F_SPECIFICATION"].ToString() + ","MODEL":" + obj_Data_Row["F_MODEL"].ToString() + ","LOCATION":" + obj_Data_Row["F_LOCATION"].ToString() + ","STD WEIGHT":" + obj_Data_Row["F_STD_WEIGHT"].ToString() + ","THICKNESS":" + obj_Data_Row["F_THICKNESS"].ToString() + ","WIDTH":" + obj_Data_Row["F_WIDTH"].ToString() + ","HEIGHT":" + obj_Data_Row["F_HEIGHT"].ToString() + ","STUFF QUALITY":" + obj_Data_Row["F_STUFF_QTY"].ToString() + ","FREIGHT":" + obj_Data_Row["F_FREIGHT"].ToString() + ","THRESHOLD FG":" + obj_Data_Row["F_THRESHOLD_FG"].ToString() + ","THRESHOLD CL STOCK":" + obj_Data_Row["F_THRESHOLD_CL_STOCK"].ToString() + ","DESCRIPTION":" + obj_Data_Row["F_DESCRIPTION"].ToString() + "}
        }
    };
于 2015-02-02T08:30:34.093 回答