tags:

views:

91

answers:

2

Hi,

I have an json output of the following (ignore the escape characters)

"{\"sEcho\":1,\"iTotalRecords\":10,\"iTotalDisplayRecords\":10,\"aaData\":[{\"Job\":\"developer\",\"Name\":\"kurt\"},{\"Job\":\"plumber\",\"Name\":\"john\"}]}"

which i get from

Person person = new Person();
            person.Name = "kurt";
            person.Job = "developer";

            Person reps2 = new Person();
            reps2.Name = "john";
            reps2.Job = "plumber";

            aa[0] = person;
            aa[1] = reps2;

             var o = new
                         {
                             sEcho = 1,
                             iTotalRecords = 10,
                             iTotalDisplayRecords = 10,
                             aaData = aa

                         };


             string d = JsonConvert.SerializeObject(o);

what I need is;

{"sEcho":1,"iTotalRecords":10,"iTotalDisplayRecords":10,"aaData":["developer","kurt"],["plumber","john"]]

someone got a nifty c# routine that I can pass on object of any kind (e.g. Person, Car, Widget etc) and it will convert it i.e. remove the object fields, curly braces etc or is there some formatting option on the Json which I can't see to do this.

The reason I need to do this is so that I can use the datatable from www.datatables.net which is expecting it in this format

thanks

+1  A: 

My guess is that instead of a Person object, you'd have to create a List for each Person, putting Person.Job as index 0 and Person.Name as index 1.

List personList = new List<string>();
personList.add("developer");
personList.add("kurt");
List reps2List = new List<string>();
reps2List.add("plumber");
reps2List.add("john");
aa[0] = personList;
aa[1] = reps2List;
Kaleb Brasee
A: 

Not sure what you mean by "object fields", but here's an example of how to take out the curly braces....

public static string MakeJsonLikeStr(object o)
{
    string json = JsonConvert.SerializeObject(o);
    return json.Replace("{", "").Replace("}", "");
}
zowens