Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- string jsonString = "{"party": [{"adults": 3,"children": [6,2]},{"adults": 2,"children": [9,5,2]}]}";
- JObject jObject = JObject.Parse(jsonString);
- JArray jRooms = (JArray)jObject["party"];
- string[] arrChilds = { };
- string value, name = string.Empty;
- int rooms = 0, adults = 0;
- foreach (JObject obj in jRooms.Children<JObject>())
- {
- foreach (JProperty singleProp in obj.Properties())
- {
- name = singleProp.Name;
- value = singleProp.Value.ToString();
- if (name == "children")
- arrChilds = singleProp.Value.ToString().Replace("[","").Replace("rn","").Replace("]","").Trim().Split(',');
- else
- adults += Convert.ToInt32(value);
- }
- rooms++;
- }
- Console.WriteLine("No. of rooms: {0} |t Total Adults: {1} |t Total Childrens: {2}", rooms, adults, arrChilds.Count() > 0 ? arrChilds.Count(): 0);
- //copy each index value to class property
- Console.WriteLine("Children1 : {0}", Convert.ToInt32(arrChilds[0]));
- Console.WriteLine("Children2 : {0}", Convert.ToInt32(arrChilds[1]));
- Console.WriteLine("Children3 : {0}", Convert.ToInt32(arrChilds[2]));
- Console.WriteLine("Children4 : {0}", Convert.ToInt32(arrChilds[3]));
- if(Convert.ToInt32(arrChilds.Count())>4) //if greater than 4 then put remaining count value to ExtraChild
- Console.WriteLine("ExtraChild : {0}", Convert.ToInt32(arrChilds.Count() - 4));
- Console.ReadLine();
- }
- class Children
- {
- private int _children1;
- private int _children2;
- private int _children3;
- private int _children4;
- public int Children1
- {
- get { return _children1; }
- set { _children1 = value; }
- }
- public int Children2
- {
- get { return _children2; }
- set { _children2 = value; }
- }
- public int Children3
- {
- get { return _children3; }
- set { _children3 = value; }
- }
- public int Children4
- {
- get { return _children4; }
- set { _children4 = value; }
- }
- }
Add Comment
Please, Sign In to add comment