Rotate C # object to json string how to handle double quote

I am using Newtonsoft.Json to parse an object into a json string. It returns something like this:

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

This is not a valid json string, I think anyone can help me?

I want something like this:

 {"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; } 

EDIT: this is the real json from the Fidder TextView answer that I see:

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

the scenario is this: I put the serialized json string in the apr CreateResponse method on the Internet. I see the response line in fidder, as I said in the question, which is not valid json

 Request.CreateResponse(HttpStatusCode.Created, returnString); 

returnString is a json string from a serialized ResponseString object

I do not think this is a valid string, am I wrong?

+6
source share
2 answers

finally fix it. share with you guys.

The root cause:
I assume this is a dual serialization issue. It seems that the ASP.NET web api 2 framework will do serialization automatically for us . and so I have SerializeObject and then Debug.Write(json) string, it works well.

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

but after the violinist calls the web api, the web api returned an invalid json (\ ") response, as I said above, this happened on other clients like ios, android devices.

because the web api is serializing for me and I am doing additional explicit serialization as well string json = JsonConvert.SerializeObject(rs); , which means that I am running another parseJson that is not needed.

in my question here, I just directly put an object that is not serializable in the CreateResponse . Request.CreateResponse(HttpStatusCode.Created, rs); And it returns valid json for feeder and other clients.

How to 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; } 

and in the controller

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

I suspect you saw this in the debugger. This is not a real line, but just a view in the visual studio debugger. For example, I tested this code:

 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); } 

how str value is shown in the window of the visual studio window: enter image description here

how the actual line is printed in the console: enter image description here

Conclusion: Newtonsoft.Json should have converted the object to the correct json string representation. No extra effort is required.

UPDATE:

Responding to your editing, I have a strong feeling that this is again the same representation in another instrument (violinist). This \" is a representation of double quotes in many programming platforms, since simple double quotes ( " ) are considered the end / beginning of a line (simply saying if you skipped this information).

0
source

All Articles