Robert
Robert

Reputation: 75

Unable to Deserialize Json using C# Rest Sharp

I am unable to deserialize a Json object via C#.

However, when deserialzing the data I receive an error message:

Content returned from Json Call:

[\r\n  {\r\n    \"postLogoutRedirectUri\": \"https://demo-pec.stage.reds.rxweb-pre.com/services/rxauthlogout\",\r\n    \"redirectUri\": \"https://demo-pec.stage.reds.rxweb-pre.com/services/rxauth\"\r\n  },\r\n  {\r\n    \"postLogoutRedirectUri\": \"https://stage.ecommsummitexpo.com/services/rxauthlogout\",\r\n    \"redirectUri\": \"https://stage.ecommsummitexpo.com/services/rxauth\"\r\n  },\r\n  {\r\n    \"postLogoutRedirectUri\": \"https://eventportal-new-stage.aem.rxweb-pre.com/services/rxauthlogout\"\r\n  }\r\n]

My RestSharp call:

public Rootobject getRedirectUris(string redirectUriGetUrl)
{
    var client = new RestClient(redirectUriGetUrl);
    var request = new RestRequest(Method.GET);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("accept", "text/plain");
    IRestResponse test = client.Execute(request);

    Rootobject object1 = JsonConvert.DeserializeObject<Rootobject>(test.Content); ;

    return object1;
}

My C# Classes

public class Rootobject
{
    public RedirectUriGetDto[] redirectUris { get; set; }
}

public class RedirectUriGetDto
{
    public string postLogoutRedirectUri { get; set; }
    public string redirectUri { get; set; }
}

I am receiving the exception message: Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 4.

Any ideas why this is occurring? Is it due to the above not being valid Json?

Any help would be greatly appreciated.

Thanks Rob

Upvotes: 1

Views: 347

Answers (2)

Abdellah Azizi
Abdellah Azizi

Reputation: 370

You have to add parent Object in Json result:

    var object1 = JsonConvert.DeserializeObject<Rootobject>(string.Format ("{{\"redirectUris\" :{0}}}", test.Content));

Upvotes: 0

D-Shih
D-Shih

Reputation: 46219

Because of your JSON data is an array doesn't object. You can try to use IEnumerable<RedirectUriGetDto> instead of Rootobject

var result = JsonConvert.DeserializeObject<IEnumerable<RedirectUriGetDto>>(test.Content); 

Upvotes: 4

Related Questions