将C#对象转换为JSON字符串,如何处理双引号

6
我正在使用Newtonsoft.Json将对象解析为JSON字符串。它返回类似于以下内容的结果:
{\"code\":-1,\"idName\":\"empty\",\"idValue\":0,\"message\":\"Failed,can not read object from body\"}

我认为这不是一个有效的JSON字符串,有没有人可以帮我解决?

我想要的是像这样的东西:

{"code":-1,"idName":"empty\",\"idValue\":0,\"message\":\"Failed,can not read object from body\"}  

public static class CommonUtility
{
    // format response string
    public static string FormatResponseString(int code, string idName, long idValue, string message)
    {
        ResponseString rs = new ResponseString();
        rs.code = code;
        rs.idName = idName;
        rs.idValue = idValue;
        rs.message = message;

        string json = JsonConvert.SerializeObject(rs);
        return json;
    }
}

public class ResponseString
{
    public int code;
    public string idName;
    public long idValue;
    public string message;
}

编辑:这是我可以在响应Fidder TextView中看到的实际JSON:

"{\"code\":-1,\"idName\":\"empty\",\"idValue\":0,\"message\":\"Failed,can not read object from body\"}"

情景如下: 我将序列化的JSON字符串放在Web API CreateResponse方法中。 我可以在Fiddler中看到响应字符串,正如我在问题中所说的那样,这不是有效的JSON。

Request.CreateResponse(HttpStatusCode.Created, returnString);

returnString 是从序列化的 ResponseString 对象中得到的 json 字符串。

我认为这不是一个有效的字符串,我错了吗?


你展示给我们的字符串是来自调试器中的工具提示吗? - Dirk
@Dirk,使用Fidder而不是Debugger。服务器API是ASP.NET Web API 2。 - max
https://github.com/Factual/factual-csharp-driver/issues/16#issuecomment-11182463 - Srinivas
2个回答

3

最终,修复了这个问题。与大家分享。

根本原因:
我猜测这是双重序列化问题。 似乎ASP.NET web api 2框架将自动为我们序列化。这就是为什么我使用SerializeObject然后Debug.Write(json)输出字符串,它可以正常工作的原因。

string json = JsonConvert.SerializeObject(rs);     
Debug.Write(json);

但是在Fiddler调用Web API后,Web API返回了无效的JSON(\")响应,就像我之前所说的那样。其他客户端,如iOS、Android设备也发生了同样的问题。
因为Web API已经为我进行了序列化,而我又做了额外的显式序列化,即string json = JsonConvert.SerializeObject(rs);,这意味着我运行了另一个不必要的parseJson。
根据我的问题,我直接将未序列化的对象放入CreateResponse方法中。Request.CreateResponse(HttpStatusCode.Created, rs);然后它会为Fiddler和其他客户端返回有效的JSON。
如何解决这个问题: Request.CreateResponse(HttpStatusCode.Created, rs);
public static class CommonUtility
    {
        // format response string
        public static ResponseString FormatResponseString(int code, string idName, long idValue, string message)
        {
            ResponseString rs = new ResponseString();
            rs.code = code;
            rs.idName = idName;
            rs.idValue = idValue;
            rs.message = message;

            return rs ;
        }
    }

    public class ResponseString
    {
        public int code;
        public string idName;
        public long idValue;
        public string message;
    }

并且在控制器中

ResponseString rs = new ResponseString();
                        rs = CommonUtility.FormatResponseString(0, "pacelId", returnPacelId, "Succeed,created items in db success");
                        return Request.CreateResponse(HttpStatusCode.Created, rs);

1
我猜你是在调试器中看到了那个。它不是实际的字符串,只是在Visual Studio调试器中的表示。例如,我用这段代码进行了测试:
private static void Main(string[] args)
{
    var station = new Station {Name = "Duren Kalibata", LastTemperature = 45, MostRecentDate = DateTime.Now};
    var str = JsonConvert.SerializeObject(station);
    Console.WriteLine(str);
}

在Visual Studio监视窗口中如何显示str值: enter image description here 在控制台中实际打印的字符串是什么: enter image description here 结论:Newtonsoft.Json 应将对象转换为其正确的 JSON 字符串表示。不需要进一步努力。
更新:
针对您的编辑,我强烈感觉这只是另一个工具(Fiddler)中的相同表示法。这个\"是许多编程平台中双引号的表示形式,因为普通的双引号(")被认为是字符串的结束/开始(只是说,以防你错过了那个信息)。

谢谢更新,我知道 " 是双引号的表示。问题在于,安卓程序员说她收到的也是无效的 JSON 字符串。她试图将其转换为对象,但遇到了无效的 JSON 格式异常。 - max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接