2

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.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
Ron F
  • 27
  • 1
  • 6

3 Answers3

1

Via http://json2csharp.com/

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; }
}
Blue Eyed Behemoth
  • 3,692
  • 1
  • 17
  • 27
0

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 { /.../ }
}
David
  • 208,112
  • 36
  • 198
  • 279
  • Thank you all. Now I get it. The problem I face know is that I have 4 files of JSON each one 500-800MB. How can I work with those files? – Ron F Oct 20 '16 at 13:57
  • @RonF: That sounds like a separate unrelated question, likely worthy of its own Stack Overflow post. You'd want to elaborate on what you've tried and what problem you're facing. In general large files work the same way as small files, just with more data. – David Oct 20 '16 at 14:00
  • Thank you. I will search about it – Ron F Oct 20 '16 at 14:01
0

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; }
}
Jack
  • 14
  • 1