0

I have a piece of code which I can't find the issue in. I'm working with Visual Studio 2008 and developing using .Net CF 3.5.

I receive the following JSON string:

[
  {
    "id": 1,
    "DbServerInstance": "Ej8/Pz9ubnldPx9ePxk=",
    "DbServerUser": "Px4/Ez8/Pz8/DT9OfBo=",
    "DbServerPassword": "TWsYRiQ/PyM/ZT8/PzJZ",
    "DbServerDatabase": "PwVRczpEPz8aPz8/DnRD",
    "UserManualURL": "Pz8rNUd2PxdAPzM/Uxw/Wg==",
    "ApplicationName": "PzwAAm5MTks/Pz0obD9P"
  }
]

I also created the following classes:

public class JSONResponse{
    [JsonProperty(PropertyName = "id")]
    public int id { get; set; }

    [JsonProperty(PropertyName = "DbServerInstance")]
    public string dbServerInstance { get; set; }

    [JsonProperty(PropertyName = "DbServerUser")]
    public string dbServerUser { get; set; }

    [JsonProperty(PropertyName = "DbServerPassword")]
    public string dbServerPassword { get; set; }

    [JsonProperty(PropertyName = "DbServerDatabase")]
    public string dbServerDatabase { get; set; }

    [JsonProperty(PropertyName = "UserManualURL")]
    public string userManualURL { get; set; }

    [JsonProperty(PropertyName = "ApplicationName")]
    public string applicationName { get; set; }
}

public class JSONArray
{
    public IList<JSONResponse> Params { get; set; }
}

I try to deserialize using the following command:

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);

This give me the error:

{"Error converting value \"[\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]\" to type 'System.Collections.Generic.List`1[louisapp.JSONResponse]'."} System.Exception {Newtonsoft.Json.JsonSerializationException}

I searched, but can't find the issue and as I'm quite new to JSON, my knowledge is limited. Any input would be appreciated.

Edit: The code which is calling the web api:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.ContentType = @"application/json";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            return sr.ReadToEnd();
        }
        else
        {
            return null;
        }
    }
6
  • 1
    echo out returnString. Are you sure that \r\n and stuff is being properly turned into new lines? It looks like you're trying to pass the escaped version to DeserializeObject Commented Sep 11, 2019 at 11:06
  • What do you mean by 'echo out'? Do you mean that I should 'unescape' it somehow? Commented Sep 11, 2019 at 11:11
  • I tried your exact code with your classes and it seems to be working: dotnetfiddle.net/6hvtv8 .The only exception I feel is that your returnString is not giving correct data as JSON. You would need to investigate into that. Commented Sep 11, 2019 at 11:29
  • What do you mean, that returnString is not correct data as JSON? That it's corrupt? I wrote a service which is returning this JSON string, using Newtonsoft aswell and when I call the service from a browser, it shows up perfectly. I have a feeling that I need to clean up the escape chars somehow, but it's now really happening Commented Sep 11, 2019 at 11:32
  • When I inspect the returnString, this is the value: [\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]. Commented Sep 11, 2019 at 11:34

4 Answers 4

4

Looks like the returnString is not in right json format. Looking at the exception the returnString having additional double quotes at begining and end \"[ ]\"

I have tried the same example and it throws the above exception

string returnString = "\"[\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]\"";

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);

After removing additional quotes, it started working

string returnString = "[\r\n {\r\n \"id\": 1,\r\n \"DbServerInstance\": \"Ej8/Pz9ubnldPx9ePxk=\",\r\n \"DbServerUser\": \"Px4/Ez8/Pz8/DT9OfBo=\",\r\n \"DbServerPassword\": \"TWsYRiQ/PyM/ZT8/PzJZ\",\r\n \"DbServerDatabase\": \"PwVRczpEPz8aPz8/DnRD\",\r\n \"UserManualURL\": \"Pz8rNUd2PxdAPzM/Uxw/Wg==\",\r\n \"ApplicationName\": \"PzwAAm5MTks/Pz0obD9P\"\r\n }\r\n]";

List<JSONResponse> rp = JsonConvert.DeserializeObject<List<JSONResponse>>(returnString);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, indeed. You're right. Do you have any idea why this "\ is added to the string? The json string is created using Newtonsoft JSON in .Net and returned via an httprequest. If possible, I would prefer to fix the issue at the source iso clipping the string. Thanks!
The possible reason could be double serialization. Just check the code whether the serialization applied twice.
On the server side it seems ok. I just debugged it and the result is a clean JSON string. When calling the web api directly in the browser it also looks clean: [{"id":1,"DbServerInstance":"Ej8/Pz9ubnldPx9ePxk=","DbServerUser":"Px4/Ez8/Pz8/DT9OfBo=","DbServerPassword":"TWsYRiQ/PyM/ZT8/PzJZ","DbServerDatabase":"PwVRczpEPz8aPz8/DnRD","UserManualURL":"Pz8rNUd2PxdAPzM/Uxw/Wg==","ApplicationName":"PzwAAm5MTks/Pz0obD9P"}] . I edited the original post with the json request code which I'm using. Could that be the culprit?
YES! It was double serialization. Thanks a lot for the hints.
hi @KrishnaVarma, I call another API by : getUrl = http.xxx.yyy RestClient client = new RestClient(getUrl); RestRequest request = new RestRequest(Method.GET); var response = await client.ExecuteTaskAsync(request); But it give me additional quotes like your first example, how can I remove it (Im not use serialization so I can't check)
0
List<JSONResponse> rp =    jserial.Deserialize<List<JSONResponse>>(returnString);

Try this one its working for us fine here

Comments

0

Additional, Double serialization occurs when you serialize anonymous type.

var anonymous  = new { Id = 1, Name = "Any Body" };

var jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(anonymous);
return Ok(jsonContent);

Result:  "{\"Id\":1,\"Name\":\"Any Body\"}"

If you return direct anonymous

return Ok(anonymous);
Result: {"id":1,"name":"Any Body"}

Comments

0

In case of double serialization of anonymous type you can use this example for avoiding it:

public object MethodAcceptingObject(object data)
    {
        //var stringData = JsonConvert.SerializeObject(data); //makes to raise your exception
        var request = JsonConvert.DeserializeObject<YourConcreteType>((string)data); //instead of deserializing stringData
...

Comments

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.