Reputation: 27
If I have a JSON file
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
, I want to use the serializer. I understand that I need to create a class that fits the JSON categories.
So I created this:
class Person
{
public String firstName;
public String lastName;
public String age;
public class address
{
public String streetAddress;
public String city;
public String state;
public String postalCode;
}
public class phoneNumber
{
public String type;
public String number;
}
}
It works fine with age and name but not with address and phoneNumber(I din't know how to creat them in the class file). I hope you can help me.
Upvotes: 2
Views: 67
Reputation: 14
Use getters and setters
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; }
}
public class Address
{
public string streetAddress { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}
public class PhoneNumber
{
public string type { get; set; }
public string number { get; set; }
}
Upvotes: 0
Reputation: 218950
You didn't create properties for the address and phone number. There has to be a target property in order to put data into that property.
Something like this:
class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; } // here
public IEnumerable<PhoneNumber> phoneNumber { get; set; } // and here
public class Address { /.../ }
public class PhoneNumber { /.../ }
}
Upvotes: 0
Reputation: 3872
public class Address
{
public string streetAddress { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}
public class PhoneNumber
{
public string type { get; set; }
public string number { get; set; }
}
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; }
public List<PhoneNumber> phoneNumber { get; set; }
}
Upvotes: 1