打开C#对象JSON字符串,如何处理双引号 [英] Turn C# object to json string, how to handle double quotation
问题描述
我使用Newtonsoft.Json解析对象JSON字符串。它somethink返回是这样的:
I'm using Newtonsoft.Json to parse object to json string. It returns somethink like this:
{\"code\":-1,\"idName\":\"empty\",\"idValue\":0,\"message\":\"Failed,can not read object from body\"}
这不是有效的JSON字符串,我认为,任何人都可以工作,我出去?
it is not a valid json string i think, anyone can work me out?
我要的是这样的:
{"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:
this is actual json from the response fidder TextView I can see:
"{\"code\":-1,\"idName\":\"empty\",\"idValue\":0,\"message\":\"Failed,can not read object from body\"}"
情景是这样的:
我把网页API CreateResponse方法序列化的JSON字符串。我可以看到Fidder酒店的响应串像我这是不是有效的JSON
the scenario is this: I put the serialized json string in web api CreateResponse method. I can see the response string in fidder like I said in the question which is not valid json
Request.CreateResponse(HttpStatusCode.Created, returnString);
returnString
是序列化的JSON字符串 ResponseString
对象
returnString
is json string from the serialized ResponseString
object
我DONOT认为这是一个有效的字符串,我错了?
I donot think it is a valid string , am I wrong?
推荐答案
最后,解决这个问题。与你们分享
finally, fix this. share with you guys.
根本原因:结果
我的猜测是,它是双系列化问题。
似乎的ASP.NET Web API 2框架会自动为我们做了连载。这就是为什么我 SerializeObject
然后 Debug.Write(JSON)
字符串,它工作得很好。
Root Cause:
My guess is that it is the double serialization issue.
It seems ASP.NET web api 2 framework will do the serialize automatically for us. and that is why I SerializeObject
and then Debug.Write(json)
the string, it works well.
string json = JsonConvert.SerializeObject(rs);
Debug.Write(json);
但小提琴手后调用web API,网页APIreturned响应无效的JSON(\),因为我上面说,这件事发生在其他客户端例如iOS,Android设备一样。
but after fiddler invoke the web API, web APIreturned response with invalid json(\") as i said above. this happened the same on other clients such as ios, android devices.
因为Web API做的系列化我,我做一个额外的显式序列也 JSON字符串= JsonConvert.SerializeObject(RS);
这意味着我运行的是不是需要另一个parseJson
because the web api do the serialization for me, and i do an extra explicit serialization also string json = JsonConvert.SerializeObject(rs);
that means i run another parseJson which is not needed.
每我的问题在这里,我只是直接就把这不是在 CreateResponse
方法序列化对象 Request.CreateResponse(HttpStatusCode.Created,RS);
键,它返回Fidder酒店和其他客户端有效的JSON
per my question here, i just directly put the object which is not serialized in the CreateResponse
method. Request.CreateResponse(HttpStatusCode.Created, rs);
And it returns valid json for the fidder and other clients.
我要如何解决这个问题:
Request.CreateResponse(HttpStatusCode.Created,RS);
How do i fix this problem: 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);
这篇关于打开C#对象JSON字符串,如何处理双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!