使用string.Replace替换json字符串中的字符和双引号和冒号?

时间:2016-02-16 14:56:05

标签: c# string

我正在使用字典创建一个json字符串,我只需要删除字符串中的那部分我的字符串是

[{Id: "code": "AAA" ,  Title: "display": "ANAA,FRENCH POLYNESIA"},{Id: "code": "AAB" ,  Title: "display": "ARRABURY, QL AUSTRALIA"}]

我只想删除

"code":

那部分来自使用string.Replace的字符串('',"")

"display":

我正在尝试这个:

        var entries = dict.Select(d =>  string.Format("{{Id: {0} ,  Title: {1}}}", d.Key, string.Join(",", d.Value)));
        return "" + string.Join(",", entries) + "";

不努力实现

[{Id: "AAA" ,  Title:  "ANAA,FRENCH POLYNESIA"},{Id:  "AAB" ,  Title:  "ARRABURY, QL AUSTRALIA"}]

2 个答案:

答案 0 :(得分:1)

您可以运行此代码:

 string json = "[{Id: \"code\": \"AAA\" ,  Title: \"display\": \"ANAA,FRENCH POLYNESIA\"},{Id: \"code\": \"AAB\" ,  Title: \"display\": \"ARRABURY, QL AUSTRALIA\"}]";
 json = json.Replace("\"code\":", String.Empty);
 json = json.Replace("\"display\":", String.Empty);

如果使用 String.Empty

,可以使用replace方法删除

答案 1 :(得分:0)

根据建议,使用string.Replace:

const string codeToRemove = "\"code\":";
const string displayToRemove = "\"display\":";
var entries = dict.Select(d =>  string.Format("{{Id: {0} ,  Title: {1}}}", d.Key.Replace(codeToRemove, ""), string.Join(",", d.Value.Replace(displayToRemove, ""))));
var result = "" + string.Join(",", entries) + "";