Creating a JSON array in C#

前端 未结 4 1941
北恋
北恋 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:36

    You're close. This should do the trick:

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

    If your source was an enumerable of some sort, you might want to do this:

    new {items = source.Select(item => new 
    {
        name = item.Name, index = item.Index, options = item.Optional
    })};
    
    0 讨论(0)
  • 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"}]}

    0 讨论(0)
  • 2021-01-30 02:51
    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() + "}
            }
        };
    
    0 讨论(0)
  • 2021-01-30 03:03

    You'd better create some class for each item instead of using anonymous objects. And in object you're serializing you should have array of those items. E.g.:

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

    Usage:

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

    And in the result you won't have to change things several times if you need to change data-structure.

    p.s. Here's very nice tool for complex jsons

    0 讨论(0)
提交回复
热议问题