-1

I need a final json format as follows and that should be dynamic.

{
      "product_items" : 
      [
        { 
          "_at" : 1,                  
          "product_id" : "999"
        },     
        {
          "_at" : 2,
          "quantity" : 2.00
        },
        {
          "_delete_at" : 3       
        }
      ]
    }

How to create a json format as above in the code._at field is dynamic.sometimes it might be 2 and sometimes it might be 10.I dont have idea on to generate the json dynamically in c#.

class Test
    {
        public ProductItem[] product_items { get; set; }


        class ProductItem
        {
            public int[] _at { get; set; }
            public int[] _delete { get; set; }
            public int[] quantity { get; set; }
            public string[] product_id{get;set;}
        }
    }

i have create the the properties for json as above.

Wiki
  • 92
  • 1
  • 8
  • 1
    Please elaborate on ***code._at field is dynamic.sometimes it might be 2 and sometimes it might be 10*** – Satpal Jan 06 '14 at 07:31
  • now i created a json with 2 [_at] times.it will keep on changing. – Wiki Jan 06 '14 at 07:32
  • You mean like `{ "_at" : 1,"_at" : 2, "_at" : 3 }`. It will be an invalid JSON – Satpal Jan 06 '14 at 07:33
  • 1
    possible duplicate of [How do i make formatted json in C#.NET](http://stackoverflow.com/questions/2661063/how-do-i-make-formatted-json-in-c-net) – Yanshof Jan 06 '14 at 07:35

2 Answers2

1

I'm using Newtonsoft library

Your class should look more like this:

public class ProductItem
{
    public int _at { get; set; }
    public string product_id { get; set; }
    public double? quantity { get; set; }
    public int? _delete_at { get; set; }
}

public class ProductItemObject
{
    public List<ProductItem> product_items { get; set; }
}

A example on serializing :

List<ProductItem> list = new List<ProductItem>();   
ProductItemObject o = new ProductItemObject { product_items = list };

var item1 = new ProductItem { _at = 1, product_id = "001" };
var item2 = new ProductItem { _at = 2, quantity = 2.00 };
var item3 = new ProductItem { _delete_at = 3 };

list.Add(item1);
list.Add(item2);
list.Add(item3);


string json = JsonConvert.SerializeObject(o, Formatting.Indented);

// json string :
//            {
//  "product_items": [
//    {
//      "_at": 1,
//      "product_id": "001",
//      "quantity": null,
//      "_delete_at": null
//    },
//    {
//      "_at": 2,
//      "product_id": null,
//      "quantity": 2.0,
//      "_delete_at": null
//    },
//    {
//      "_at": 0,
//      "product_id": null,
//      "quantity": null,
//      "_delete_at": 3
//    }
//  ]
//}

An alternative full dynamic that gets u the same Json string without any model :

var jsonObject = new JObject();
dynamic objectList = jsonObject;

objectList.product_items = new JArray() as dynamic;

dynamic item = new JObject();
item._at = 1;
item.product_id = "999";
objectList.product_items.Add(item);

item = new JObject();
item._at = 2;
item.quantity = 2.00;
objectList.product_items.Add(item);

item = new JObject();
item._delete_at = 3;
objectList.product_items.Add(item);

string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, Formatting.Indented);
Jim
  • 2,974
  • 2
  • 19
  • 29
0

Well, if I understand you properly and you just need to be able to generate that json, the product list should be dynamic with maybe anonymous classes then:

public class Products
{
   public Products()
   {
       product_items = new List<dynamic>();
   }
   public List<dynamic> product_items { get; set; }
}

products.product_items.Add(new { _at = 1, product_id = "999" });
Karhgath
  • 1,809
  • 15
  • 11