6

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\"}

it is not a valid json string i think, anyone can work me out?

What I want is 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 actual json from the response fidder TextView I can 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 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 is json string from the serialized ResponseString object

I donot think it is a valid string , am I wrong?

3
  • Is the string you showed us from the tooltip in the debugger? Commented Mar 10, 2014 at 6:26
  • @Dirk, from the fidder instead of debugger. server api is asp.net web api 2 Commented Mar 10, 2014 at 9:13
  • github.com/Factual/factual-csharp-driver/issues/… Commented Feb 11, 2016 at 7:01

2 Answers 2

3

finally, fix this. share with you guys.

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

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.

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.

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.

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

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);
Sign up to request clarification or add additional context in comments.

Comments

1

I suspect you saw that in debugger. It isn't the actual string, just representation in visual studio debugger. For example, I tested with 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 shown in visual studio watch window : enter image description here

how the actual string printed in console : enter image description here

Conclusion : Newtonsoft.Json should've converted the object to it's correct json string representation. No further effort needed.

UPDATE :

Responding to your edit, I have a strong feeling that it is, again, just same representation in another tool (fiddler). This \" is representation of double-quotes in many programming platform, because plain double-quotes (") is considered end/beginning of string (just saying, in case you missed that info).

1 Comment

thnx for update, i konw \" is representation of double-quotes.here is the problem, the android programer said what she received is also the invalid json string. he try to object the json stirng and she got exception with invalid json format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.