1

Hi all i want to convert a list which contain 36 rows of value i want to change this in specific format like this

"rows": [{
    "id": 1,
    "cell": ["1", "Super Item", "300", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test27", 0, null, false, false]
  }, {
    "id": 2,
    "cell": ["2", "Item 1", "100", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 1, 1, false, false]
  }, {
    "id": 3,
    "cell": ["3", "Sub Item 1", "50", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 2, 2, true, true]
  }, {
    "id": 4,
    "cell": ["4", "Sub Item 2", "25", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 2, 2, false, false]
  }, {
    "id": 5,
    "cell": ["5", "Sub-sub Item 1", "25", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 3, 4, true, true]
  }, {
    "id": 6,
    "cell": ["6", "Sub Item 3", "25", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 2, 2, true, true]
  }, {
    "id": 7,
    "cell": ["7", "Item 2", "200", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 0, null, false, false]
  }, {
    "id": 8,
    "cell": ["8", "Sub Item 1", "100", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 1, 7, false, false]
  }, {
    "id": 9,
    "cell": ["9", "Sub-sub Item 1", "50", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 2, 8, true, true]
  }, {
    "id": 10,
    "cell": ["10", "Sub-sub Item 2", "50", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 1, 8, false, true]
  }, {
    "id": 11,
    "cell": ["11", "Sub Item 2", "100", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 2, 8, true, true]
  }, {
    "id": 12,
    "cell": ["12", "Super Item", "300", "test", "test1", "test2,", "test3,", "test4", "test5", "test6", "test7", "test8", "test9", "test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20", "test21", "test22", "test23", "test24", "test25", "test26", "test26", 0, null, false, false]
  }
]

how can i chage this list into like this format of json

var res = new List<FlatProduct>();

i have a list of flatproduct .

1
  • It would be nice to see your FlatProduct object, we can merly guess what the properties are representing now Commented Apr 9, 2013 at 7:59

2 Answers 2

2

JSON serialization is supported out of the box from .NET 3.5. You have to reference System.Runtime.Serialization and System.ServiceModel.Web.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;

namespace ConsoleApplication1
{
    [DataContract]
    public class FlatProduct
    {
        [DataMember]
        public string id { get; set; }

        [DataMember]
        public string[] cell { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var flatProducts = new List<FlatProduct>
            {
                new FlatProduct { id = "1", cell = new string[2]{"1", "Super Item"} },
                new FlatProduct { id = "2", cell = new string[2]{"2", "Item 1"} }
            };
            var serializer = new DataContractJsonSerializer(flatProducts.GetType());
            using(MemoryStream stream = new MemoryStream())
            {
                serializer.WriteObject(stream, flatProducts);
                stream.Seek(0, SeekOrigin.Begin);

                using (var streamReader = new StreamReader(stream))
                {
                    string result = streamReader.ReadToEnd();
                }
            }
        }
    }
}

Of course, you can put more items in the string[] of FlatProduct.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use Json.NET from codeplex

http://json.codeplex.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.