0

I just started setting up a framework for testing the API's and I'm using specflow with C# and VS. I'm able to send a request and receive a JSON response, however when I tried to Deserialise the response.Content I am getting an error. I think it is because the response.Content string has forward slashes in them as shown below. I tried removing them before deserializing but it does not remove it as I can see it in the debugging mode.

Error I get is: An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Unexpected character encountered while parsing value: . Path '', line 0, position 0.

respJson - "{"Result":{"isLinked":true,"saleDateTime":"16/06/2017 14:20:20","storeName":"UAT1 BRIGHTON LOC 4"},"Status":{"ActionType":0,"IsSuccess":true,"ActionString":""}}"

Method - public static dynamic receiptRetrieve(string uri, Method method, string inputJsonFileName, Dictionary inputParams = null){

 object temp = GetJsonInput(inputJsonFileName, inputParams);
 var response = RestFeedPlugin.SendRequestToEReceiptsAPI(uri, method, null, temp);


 string respJson = response.Content.Replace(@"\", @"");
 dynamic jsonObject = JsonConvert.DeserializeObject(respJson);

 return jsonObject;

}

I figured what the issue is. The json responseI'm getting from RestSharp is adding a space at the start which causes it to break. If I remove the first character from the response.content it seems to be working. However, this is not an ideal solution and I'm still open to thoughts on why this is happening at the first place? Temporary solution to my problem -

        var response = client.Execute(request);
        response.Content = response.Content.Remove(0, 1);
        client.BuildUri(request);
Fishyish
  • 21
  • 4
  • I think what you need is here: https://stackoverflow.com/a/13866117/1699071 – SAm Aug 28 '17 at 02:23
  • do not replace the backslash because it makes the double quotes included as a string as we declare string as "The string" so it automatically makes the double quotes possible, this is called escape string. The problem is how you deserialize your Json. Why do you have dynamic type in the first place? – Aizen Aug 28 '17 at 08:35
  • Actually I figured what the issue is. The json responseI'm getting from RestSharp is adding a space at the start which causes it to break. If I remove the first character from the response.content it seems to be working. However, this is not an ideal solution and I'm still open to thoughts on why this is happening at the first place? – Fishyish Aug 29 '17 at 01:02

1 Answers1

0

Those backslashes are escaping the apostrophes inside the text. They are meant to be there. Your program works fine without removing them:

using System;
using Newtonsoft.Json;

public class Program
{

    public static void Main()
    {
        string json =  "{\"Result\":{\"isLinked\":true,\"saleDateTime\":\"16/06/2017 14:20:20\",\"storeName\":\"UAT1 BRIGHTON LOC 4\"},\"Status\":{\"ActionType\":0,\"IsSuccess\":true,\"ActionString\":\"\"}}";
        dynamic jsonObject = JsonConvert.DeserializeObject(json);

        Console.WriteLine(jsonObject);
    }
}

You can see it in action here: https://dotnetfiddle.net/rJwaJM

Palle Due
  • 4,235
  • 3
  • 16
  • 29