Search code examples
c#.netjsonjson.netcompact-framework

'Error converting value' when JSON DeserializeObject


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

Solution

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