0

I am Writing a Code using C# that will Parse JSON Code and create it as objects.

I have this JSON Example

{
     "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"
         }
     ]
 }

as you can see there is multible numbers, some Person have 1 telephone numbers, some others has 4 or 5.

I know i should do a While Loop, but can i get how many Phones are inside the Phone number to use it a counter to add these phones to PhoneNumber Object that i created?

6
  • 2
    phoneNumber is an Array of {type" : "..","number": ".." } Commented Jun 4, 2012 at 11:16
  • Well how are you doing the parse, do you really need to know how many phone numbers would be there ? Commented Jun 4, 2012 at 11:16
  • I am doing the parse using NEWTONSOFT.JSON, this is how i do the parse PhoneNumber is a class i created, and I have another Class Called Person that has a instance variable as List<PhoneNumber> Commented Jun 4, 2012 at 11:18
  • Are you doing as an exercise or a homework? Because there are libraries which already do this for you. Commented Jun 4, 2012 at 11:18
  • 1
    Take a look over here stackoverflow.com/questions/9984337/… Commented Jun 4, 2012 at 11:23

2 Answers 2

0

Using NEWTONSOFT.JSON, you can get an array of PhoneNumber by this code

JArray sa = (JArray)o["person"]["phoneNumbers"]["phoneNumber"];

and this get number of phonenumber availables by

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

Comments

0

If you are using Json.Net for parsing json, than first you should create the following create and and deserialize the json into the following Contact Object, after deserializing into the contact object every thing will be straight forward.

public class Contact
{
  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; }
}

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; }
}


var contact = Newtonsoft.Json.JsonConvert.DeserializeObject<Contact>(data);
var count = contact.phonenumber.Count;

1 Comment

@Asef, regarding your solution, i think it will be the best if it work, but i tried it, the deserializeObject type contacts, it gives me null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.