简体   繁体   English

我如何将一个字符串(json)转换为另一个字符串

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

I have an json output of the following (ignore the escape characters) 我有以下json输出(忽略转义字符)

"{\"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. 有人有一个漂亮的C#例程,我可以传递任何类型的对象(例如Person,Car,Widget等),它将进行转换,即删除对象字段,花括号等,或者Json上有一些格式化选项,我可以看不到这样做。

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 我需要这样做的原因是,这样我就可以使用www.datatables.net中的数据表了,它期望以这种格式

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. 我的猜测是,您必须为每个Person创建一个List,而不是Person对象,将Person.Job设置为索引0,将Person.Name设置为索引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("}", "");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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