简体   繁体   中英

how can I convert a string (json) to look like another string

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 (eg Person, Car, Widget etc) and it will convert it ie 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

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;

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("}", "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM