-2

While I am trying to parse the Json Result I get following exception:

Unexpected character encountered while parsing value: [. Path 'Data', line 1, position 26.

Here is My Json Data:

{
   "StatusCode":200,
   "Data":[
      {
         "MemberId":1,
         "CreaterUserId":5,
         "FirstName":"Saravanan",
         "LastName":"Subramanian",
         "Title":"Physician\u0019s Assistant",
         "License":"A 123447",
         "Email":"saravanan.radhamani@gmail.com",
         "PhoneNumber":"1234567",
         "IsSign":true,
         "IsMemberActive":true,
         "CreatedDate":"2018-06-23T15:45:44.843",
         "PhotoPath":null
      }
   ]
}

Here is My C# Code:

public static void Main(string[] args)
{
    string response = null;
    try
    {
        response = makeRequest();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

    var data = JsonConvert.DeserializeObject<PolstForm>(response);
    Console.WriteLine(string.Format("StatusCode : {0} , Data : {1}",
               data.StatusCode,
               data.Data[0]));
    Console.ReadKey();
}

private static string makeRequest()
{
    var client = new RestClient(urlString);
    var request = new RestRequest(Method.GET);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("accept", "application/json");
    return client.Execute(request).Content; //the Content (body) of the response
}

This is My PolstForm.class

public class PolstForm
{
    public int StatusCode { get; set; }
    public string Data { get; set; }
}

When I am trying to parse the data from json it returns an Error.

Error Shown is:

JsonReaderException is unhandled by user code : 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 'Data', line 1, position 26.

response contains above Json format.

If you have alternate thing please share with me.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mohideen Asraf
  • 32
  • 1
  • 1
  • 8
  • Possible duplicate of [Unexpected character encountered while parsing value](https://stackoverflow.com/questions/23259173/unexpected-character-encountered-while-parsing-value) – Lucifer Jul 09 '18 at 11:40
  • 1
    Are you sure that you've presented the ***actual*** JSON being parsed? What you've shown doesn't have a position 26 at line 1. – spender Jul 09 '18 at 11:41
  • The json looks fine. Did you copy it from `response` as it is or did you alter it? You may have removed some characters which create the issue ... – derpirscher Jul 09 '18 at 11:42
  • Now I am Edit my code and add PolstForm class please tell me a solution. parse a null value error..from a PhotoPath thats I think. – Mohideen Asraf Jul 09 '18 at 11:59
  • 1
    `string Data` and your Json shows an array here instead of a string. Also what do you expect `data.Data[0]` to return? – Rafalon Jul 09 '18 at 12:01

2 Answers2

4

Your PolstForm class doesn't match your Json's structure.

I would've written it this way for example:

public class PolstForm
{
    public int StatusCode { get; set; }
    public CustomData[] Data { get; set; }
}

public class CustomData
{
    public int MemberId {get;set;}
    public int CreaterUserId {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public string Title {get;set;}
    public string License {get;set;}
    public string Email {get;set;}
    public string PhoneNumber {get;set;}
    public bool IsSign {get;set;}
    public bool IsMemberActive {get;set;}
    public DateTime CreatedDate {get;set;}
    public string PhotoPath {get;set;}
}
Rafalon
  • 4,450
  • 2
  • 16
  • 30
  • Hi @Rafalon I am Already found the answer in a similar way as your code.. I just taken the class as list for ex of your code .. List customData; Thank u for taking a time to think a solution for me. – Mohideen Asraf Jul 09 '18 at 16:29
  • You're welcome, I'm glad it helped you solve your problem :) – Rafalon Jul 09 '18 at 20:29
1

As you know, the json String can't parse to a String data; [public string Data { get; set; }]

 [
   {
     "MemberId":1,
     "CreaterUserId":5,
     "FirstName":"Saravanan",
     "LastName":"Subramanian",
     "Title":"Physician\u0019s Assistant",
     "License":"A 123447",
     "Email":"saravanan.radhamani@gmail.com",
     "PhoneNumber":"1234567",
     "IsSign":true,
     "IsMemberActive":true,
     "CreatedDate":"2018-06-23T15:45:44.843",
     "PhotoPath":null
  }

]

袁健凯
  • 11
  • 1
  • I'm facing same problem. It's coming from Asp.Net Core after parsing of data. You can do string manipulation here. I did it worked. – agileDev Jul 01 '20 at 09:21