Guest User

Untitled

a guest
Mar 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. string jsonString = "{"party": [{"adults": 3,"children": [6,2]},{"adults": 2,"children": [9,5,2]}]}";
  4. JObject jObject = JObject.Parse(jsonString);
  5. JArray jRooms = (JArray)jObject["party"];
  6. string[] arrChilds = { };
  7. string value, name = string.Empty;
  8. int rooms = 0, adults = 0;
  9. foreach (JObject obj in jRooms.Children<JObject>())
  10. {
  11. foreach (JProperty singleProp in obj.Properties())
  12. {
  13. name = singleProp.Name;
  14. value = singleProp.Value.ToString();
  15. if (name == "children")
  16. arrChilds = singleProp.Value.ToString().Replace("[","").Replace("rn","").Replace("]","").Trim().Split(',');
  17. else
  18. adults += Convert.ToInt32(value);
  19. }
  20. rooms++;
  21. }
  22. Console.WriteLine("No. of rooms: {0} |t Total Adults: {1} |t Total Childrens: {2}", rooms, adults, arrChilds.Count() > 0 ? arrChilds.Count(): 0);
  23.  
  24. //copy each index value to class property
  25. Console.WriteLine("Children1 : {0}", Convert.ToInt32(arrChilds[0]));
  26. Console.WriteLine("Children2 : {0}", Convert.ToInt32(arrChilds[1]));
  27. Console.WriteLine("Children3 : {0}", Convert.ToInt32(arrChilds[2]));
  28. Console.WriteLine("Children4 : {0}", Convert.ToInt32(arrChilds[3]));
  29.  
  30. if(Convert.ToInt32(arrChilds.Count())>4) //if greater than 4 then put remaining count value to ExtraChild
  31. Console.WriteLine("ExtraChild : {0}", Convert.ToInt32(arrChilds.Count() - 4));
  32. Console.ReadLine();
  33. }
  34.  
  35. class Children
  36. {
  37. private int _children1;
  38. private int _children2;
  39. private int _children3;
  40. private int _children4;
  41.  
  42. public int Children1
  43. {
  44. get { return _children1; }
  45. set { _children1 = value; }
  46. }
  47. public int Children2
  48. {
  49. get { return _children2; }
  50. set { _children2 = value; }
  51. }
  52. public int Children3
  53. {
  54. get { return _children3; }
  55. set { _children3 = value; }
  56. }
  57.  
  58. public int Children4
  59. {
  60. get { return _children4; }
  61. set { _children4 = value; }
  62. }
  63. }
Add Comment
Please, Sign In to add comment