0

I am using the following function for converting JSON string to DataSet

public DataSet GlobalApiCall(string API_Name, string postData)
{
    ConnectionClass t1 = new ConnectionClass();
    var link = ConfigurationManager.AppSettings["APIUrl"];

    var request1 = (HttpWebRequest)WebRequest.Create(link + API_Name);
    var data1 = Encoding.ASCII.GetBytes(postData);

    request1.Method = "POST";
    request1.ContentType = "application/x-www-form-urlencoded";
    request1.ContentLength = data1.Length;

    using (var stream = request1.GetRequestStream())
    {
        stream.Write(data1, 0, data1.Length);
    }

    var response1 = (HttpWebResponse)request1.GetResponse();

    var responseString1 = new StreamReader(response1.GetResponseStream()).ReadToEnd();
    String json = responseString1.Replace("\"", "'");

    DataSet ReturnDt = JObject.Parse(json)["data"].ToObject<DataSet>();

    return ReturnDt;
}

It works fine. But, if the return value has a ' (apostrophe) it throws an error

"After parsing a value an unexpected character was encountered: <. Path"

Because am using String json = responseString1.Replace("\"", "'"); this replace. Which replace character should we use in this kind of situation?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Aju
  • 503
  • 1
  • 8
  • 26
  • 1
    Could you please provide a sample of your json value? – Nagaraj Raveendran Jul 06 '20 at 11:43
  • @NagarajRaveendran any data with ' sign on it will be an error – Aju Jul 06 '20 at 12:02
  • 3
    You should not be using any replace statement ion the JSON prior to deserializing. That is likely the cause of your error. What is the reason you are trying to replace quotes with apostrophes in the first place? – Brian Rogers Jul 06 '20 at 14:24
  • Maybe this existing article would help where they convert to dataset: https://stackoverflow.com/questions/19136024/how-to-parse-a-jsonstring-to-dataset – Geekn Jul 06 '20 at 14:33
  • Seems to me the service is returning invalid JSON and you should be talking to the purveyors of that service. – Heretic Monkey Jul 06 '20 at 14:35
  • @BrianRogers you had a point, I don't remember at what point I did that replace statement, but I did it for a reason. Commenting it, for now, let's see what happens ..!! Thanks :) – Aju Jul 07 '20 at 07:00

0 Answers0