Anand
Anand

Reputation: 1959

Json Deserialization gets error on xamarin.forms

I am trying to deserialize a json that will get when I try to login.But I am getting an error {Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 3. After Deserialze My navigation is depend on that "Message" string

The JSON I am getting(jsonObtained )

    {
  "Message": [
    {
      "Mesaage": "Authenticated"
    }
  ],
  "Entry": [
    {
      "User_Id": 0,
      "Name": "Client",
      "Client_Id": 0,
      "Role": 1,
      "LoginName": "user",
      "Email": "test@gmail.com",
      "IsInternalUser": "N"
    }
  ]
}

How Iam trying to deserialize.

 var jsonObtained = Regex.Unescape(stringObtained);
            int startIndex = jsonObtained.IndexOf('[');
            int endIndex = jsonObtained.LastIndexOf(']');
            int length = endIndex - startIndex + 1;
            var resultJSON = jsonObtained.Substring(startIndex, length);

 T resultObject;//Generic type object
            try
            {
                resultObject = JsonConvert.DeserializeObject<T>(resultJSON);//, settings);  
                removeLoadingAnimation();
                return resultObject;
            }

Any helps appreciated.

Upvotes: 3

Views: 308

Answers (1)

Nkosi
Nkosi

Reputation: 247551

The way you are trying to extract the substring is resulting in malformed JSON

It looks like the entry is dynamic based the currently shown code.

Refactor the object model to better match the expected JSON.

public partial class RootObject<T> {
    [JsonProperty("Message")]
    public Message[] Message { get; set; }

    [JsonProperty("Entry")]
    public T[] Entry { get; set; }
}

public partial class Message {
    [JsonProperty("Mesaage")]
    public string Mesaage { get; set; }
}

That way simply deserialize the response based on the expected type

//...

var jsonObtained = Regex.Unescape(stringObtained);

T resultObject;//Generic type object

try
{
    resultObject = JsonConvert.DeserializeObject<RootObject<T>>(jsonObtained);
    removeLoadingAnimation();
    return resultObject;
}

Where it is assumed in this case that T is of the desired type that matches the Entry key in the JSON

Otherwise you should create a model that matches the expected JSON

public partial class RootObject{
    [JsonProperty("Message")]
    public Message[] Message { get; set; }

    [JsonProperty("Entry")]
    public Entry[] Entry { get; set; }
}

public partial class Message {
    [JsonProperty("Mesaage")]
    public string Mesaage { get; set; }
}

public partial class Entry {
    [JsonProperty("User_Id")]
    public long UserId { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("Client_Id")]
    public long ClientId { get; set; }

    [JsonProperty("Role")]
    public long Role { get; set; }

    [JsonProperty("LoginName")]
    public string LoginName { get; set; }

    [JsonProperty("Email")]
    public string Email { get; set; }

    [JsonProperty("IsInternalUser")]
    public string IsInternalUser { get; set; }
}

and use that

//...

var jsonObtained = Regex.Unescape(stringObtained);

RootObject resultObject;

try
{
    resultObject = JsonConvert.DeserializeObject<RootObject>(jsonObtained);
    removeLoadingAnimation();
    return resultObject;
}

Upvotes: 2

Related Questions