JSON序列化将额外字符添加为" \"

时间:2015-08-12 11:03:39

标签: c# json string wcf json.net

我有一个这样的字符串,我从报告表达式中获取:

 jql = @"(Project in (""CI"") and Status in (""Open"") and issueType in (""Action Item"")) or issueKey = ""GR L-1"" order by Created asc";

我将其转换为JSON并得到如下结果:

 jql = jql.Replace("\"", "'");
 jql = "{" + "\"" + "jql" + "\"" + ": " + "\"" + jql + "\"" + "}";

低于JSON:

   {"jql": "(Project in ('CI') and Status in ('Open') and issueType in ('Action Item')) or issueKey = 'GR L-1' order by Created asc"}

当我像这样做串行化时:

string data = JsonConvert.SerializeObject(request);

它给我这样的刺痛:

 {"jql":"{\"jql\": \"(Project in ('CI') and Status in ('Open') and issueType in ('Action Item')) or issueKey = 'GR L-1' order by Created asc\"}","startAt":0,"maxResults":0,"fields":[]}

这是一种错误的字符串。其实我想要一个像这样的字符串:

{"jql": "(Project in ('CI') and Status in ('Open') and issueType in ('Action Item')) or issueKey = 'GR L-1' order by Created asc"}

注意:我需要这样做:

SearchRequest request = new SearchRequest();
request.JQL = jql;
 string data = JsonConvert.SerializeObject(request);
 string result = runQuery(JiraResource.search, data, "POST");

我试过这样的话:Escape characters after Serializing using Json 但是,这不起作用......

其实我想要一个这样的字符串:

{"jql": "(Project in ('CI') and Status in ('Open') and issueType in ('Action Item')) or issueKey = 'GR L-1' order by Created asc"}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

此代码适用于您的情况:

string jql = @"(Project in (""CI"") and Status in (""Open"") and issueType in (""Action Item"")) or issueKey = ""GR L-1"" order by Created asc";
jql = jql.Replace("\"", "'");
string result = JsonConvert.SerializeObject(new { jql = jql } );

产生输出

  

{" jql":"(项目在(' CI')和状态在(' Open')和issueType in(' ;行动项目'))或issueKey =' GR L-1'按创建的asc"}

排序

按预期。