0

I have a JSON object:

string bodyText = " {\"id\":16751112,\"firstname\":{\"value\":\"Sydni\",\"versions\":[{\"value\":\"Sydni\",\"source-type\":\"FORM\",\"source-id\":\"0eec9e33-4e82-4511-85ef-83556395e046\",\"source-label\":\"First Name\"}  ";

and a class that maps to the json object:

public class Person 
{
    public string firstname { get; set; }
    public string id { get; set; }
}

The following code populates the id property, but not the first name:

Person _Person = JsonConvert.DeserializeObject<Person>(bodyText);

Can someone please help me deserialize the fistname element in the json string object and store in in my _Person object?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

3

First of all, your posted JSON is incorrect - there is no closing ] and two } - it should be:

"{\"id\":16751112,\"firstname\":{\"value\":\"Sydni\",\"versions\":[{\"value\":\"Sydni\",\"source-type\":\"FORM\",\"source-id\":\"0eec9e33-4e82-4511-85ef-83556395e046\",\"source-label\":\"First Name\"}]}}"

Also, firstname in JSON is not string, it is object.

Your classes should be:

public class Version
{
    public string Value { get; set; }
    public string Source-type { get; set; }
    public string Source-id { get; set; }
    public string Source-label { get; set; }
}

public class Firstname
{
    public string Value { get; set; }
    public IList<Version> Versions { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public Firstname Firstname { get; set; }
}
Roman
  • 11,966
  • 10
  • 38
  • 47