Multiple Items

Dec 27, 2008 at 7:14 PM

Hello all,

I am trying to create JSON valu for the structure below via Json.NET.

{
  itemCount: 2,
  subtotal: '$15.50',
  items:[
    {
      title: 'The Big Book of Foo',
      description: 'Bestselling book of Foo by A.N. Other',
      imageUrl: '/images/books/12345.gif',
      price: '$10.00',
      qty: 1
    },
    {
      title: 'Javascript Pocket Reference',
      description: 'Handy pocket-sized reference for the Javascript language',
      imageUrl: '/images/books/56789.gif',
      price: '$5.50',
      qty: 1
    }
  ]
}

I prepared the code as shown below to handle that but i could not add second item to the Json , how can i do that ,please help me  :)

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        using (JsonWriter jsonWriter = new JsonTextWriter(sw))
        {

               jsonWriter.Formatting = Formatting.Indented;

                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("itemcount");
                jsonWriter.WriteValue(2);
                jsonWriter.WritePropertyName("subtotal");
                jsonWriter.WriteValue(15.50m);

                jsonWriter.WritePropertyName("items");
                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("title");
                jsonWriter.WriteValue("The Big Book of Foo");
                jsonWriter.WritePropertyName("description");
                jsonWriter.WriteValue("Bestselling book of Foo by A.N. Other");
                jsonWriter.WritePropertyName("imageUrl");
                jsonWriter.WriteValue("/images/books/12345.gif");
                jsonWriter.WritePropertyName("price");
                jsonWriter.WriteValue(10.00m);
                jsonWriter.WritePropertyName("qty");
                jsonWriter.WriteValue(1);
                jsonWriter.WriteEnd();

jsonWriter.WriteEndObject();
            }





Coordinator
Dec 28, 2008 at 8:10 AM
WriteStartArray...