0

This is the example json from json.net website

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

usage:

string json = @"{
  'Email': '[email protected]',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";

Account account = JsonConvert.DeserializeObject<Account>(json);

Console.WriteLine(account.Email);

But when I run it I'm getting error:

Unable to find a constructor to use for type Gogch.Account. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Email', line 1, position 9.

I added an empty constructor as said in another topic, but I'm still getting the same error. Any help is appreciated.

5
  • I added an empty constructor as said in another topic Can you show us the full code for Account, then? Commented Jun 22, 2016 at 6:03
  • The code you've given here works fine for me. Is this only giving you a problem in Xamarin, or are you able to reproduce it in a quick console app? Commented Jun 22, 2016 at 6:04
  • Works in csharppad: csharppad.com/gist/aab77cb2581b47e05fcc3488669537f0 Commented Jun 22, 2016 at 6:04
  • Is this in android? Commented Jun 22, 2016 at 6:11
  • yes, but not working in my project Commented Jun 22, 2016 at 6:16

1 Answer 1

1

There is nothing wrong with your above code... The error is definitely somewhere else. I ran your code using Newtonsoft.Json and it worked fine..Please go through the other portion of your source code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.