.NET 4.x [Newtonsoft.Json] Парсинг JSON-ответа - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте уважаемые участники сообщества "CyberForum" ! У меня возникла небольшая проблема. Я перелопатил кучу форумов и 15 страниц "Google". Дело в том, что мне надо с JSON ответа выковыривать несколько значений, пример:
Листинг программы
  1. {
  2. "response":{
  3. "count":1158,
  4. "unread_dialogs":5,
  5. "items":[
  6. {
  7. "unread":247,
  8. "message":{
  9. "id":606897,
  10. "date":1441637552,
  11. "out":0,
  12. "user_id":261238234,
  13. "read_state":0,
  14. "title":"Вова, не трогай котикаф",
  15. "body":"рип ангел",
  16. "chat_id":60,
  17. "chat_active":[
  18. 143932243,
  19. 67463579,
  20. 89780064,
  21. 156305972,
  22. 219100799,
  23. 279883080,
  24. 278401247,
  25. 63978237,
  26. 236548043,
  27. 18128006,
  28. 172913248,
  29. 223386494,
  30. 151548290,
  31. 274091233,
  32. 275731093,
  33. 261238234,
  34. 272130632,
  35. 215948747,
  36. 115902960,
  37. 179462619,
  38. 310321856,
  39. 309337547,
  40. 132601651,
  41. 73369298,
  42. 197484075,
  43. 56490870,
  44. 314949237,
  45. 145607814,
  46. 230888230,
  47. 127010641,
  48. 97159873,
  49. 227957341,
  50. 316944318,
  51. 267394592,
  52. 296070568,
  53. 158701794,
  54. 307575713,
  55. 302530258,
  56. 274043557,
  57. 262464688,
  58. 322229680
  59. ],
  60. "push_settings":{
  61. "sound":0,
  62. "disabled_until":-1
  63. },
  64. "users_count":43,
  65. "admin_id":143932243,
  66. "photo_50":"https:\/\/pp.vk.me\/c623723\/v623723341\/3d431\/1eHfQXrtN68.jpg",
  67. "photo_100":"https:\/\/pp.vk.me\/c623723\/v623723341\/3d430\/g6mdJ3OiSi4.jpg",
  68. "photo_200":"https:\/\/pp.vk.me\/c623723\/v623723341\/3d42f\/0hfcii-i9sM.jpg"
  69. }
  70. },
  71. {
  72. "unread":288,
  73. "message":{
  74. "id":606850,
  75. "date":1441637232,
  76. "out":0,
  77. "user_id":243834389,
  78. "read_state":0,
  79. "title":"Песат какойта",
  80. "body":"",
  81. "attachments":[
  82. {
  83. "type":"photo",
  84. "photo":{
  85. "id":380631736,
  86. "album_id":-3,
  87. "owner_id":243834389,
  88. "photo_75":"https:\/\/pp.vk.me\/c622426\/v622426389\/3fa70\/iSW5omEuCU8.jpg",
  89. "photo_130":"https:\/\/pp.vk.me\/c622426\/v622426389\/3fa71\/hJDwVmDYEtY.jpg",
  90. "photo_604":"https:\/\/pp.vk.me\/c622426\/v622426389\/3fa72\/5sMHgSgrAfY.jpg",
  91. "width":296,
  92. "height":99,
  93. "text":"",
  94. "date":1441637232,
  95. "access_key":"297004d56320bc2227"
  96. }
  97. }
  98. ],
  99. "chat_id":75,
  100. "chat_active":[
  101. 156305972,
  102. 278401247,
  103. 219100799,
  104. 272130632,
  105. 223386494,
  106. 158701794,
  107. 276539367,
  108. 172913248,
  109. 279883080,
  110. 89780064,
  111. 155160916,
  112. 55475785,
  113. 67788426,
  114. 279508895,
  115. 306789926,
  116. 243834389,
  117. 135789959,
  118. 288352317,
  119. 270312149,
  120. 274091233,
  121. 90735275
  122. ],
  123. "push_settings":{
  124. "sound":0,
  125. "disabled_until":-1
  126. },
  127. "users_count":22,
  128. "admin_id":156305972,
  129. "photo_50":"https:\/\/pp.vk.me\/c622126\/v622126972\/4d4c7\/0W-ma1zZ3w4.jpg",
  130. "photo_100":"https:\/\/pp.vk.me\/c622126\/v622126972\/4d4c6\/CYKgzWjXf04.jpg",
  131. "photo_200":"https:\/\/pp.vk.me\/c622126\/v622126972\/4d4c4\/TXjRF68B0jI.jpg"
  132. }
  133. },
  134. {
  135. "message":{
  136. "id":606737,
  137. "date":1441633270,
  138. "out":0,
  139. "user_id":206579645,
  140. "read_state":1,
  141. "title":" ... ",
  142. "body":"Попробуй еще"
  143. }
  144. },
  145. {
  146. "message":{
  147. "id":606731,
  148. "date":1441633098,
  149. "out":1,
  150. "user_id":158021141,
  151. "read_state":1,
  152. "title":" ... ",
  153. "body":"В полную сили били?"
  154. }
  155. },
  156. {
  157. "message":{
  158. "id":606720,
  159. "date":1441632727,
  160. "out":1,
  161. "user_id":190661725,
  162. "read_state":0,
  163. "title":" ... ",
  164. "body":"Не , завтра приду , надо документы забрать"
  165. }
  166. }
  167. ]
  168. }
  169. }
Мне нужны вытянуть отсюда значения: user_id или chat_id, body. Изасунуть их в массив. Единственное что я смог написать это:
Листинг программы
  1. public class PushSettings
  2. {
  3. public int sound { get; set; }
  4. public int disabled_until { get; set; }
  5. }
  6. public class Photo
  7. {
  8. public int id { get; set; }
  9. public int album_id { get; set; }
  10. public int owner_id { get; set; }
  11. public string photo_75 { get; set; }
  12. public string photo_130 { get; set; }
  13. public string photo_604 { get; set; }
  14. public int width { get; set; }
  15. public int height { get; set; }
  16. public string text { get; set; }
  17. public int date { get; set; }
  18. public string access_key { get; set; }
  19. }
  20. public class Attachment
  21. {
  22. public string type { get; set; }
  23. public Photo photo { get; set; }
  24. }
  25. public class Message
  26. {
  27. public int id { get; set; }
  28. public int date { get; set; }
  29. public int @out { get; set; }
  30. public int user_id { get; set; }
  31. public int read_state { get; set; }
  32. public string title { get; set; }
  33. public string body { get; set; }
  34. public int chat_id { get; set; }
  35. public List<int> chat_active { get; set; }
  36. public PushSettings push_settings { get; set; }
  37. public int users_count { get; set; }
  38. public int admin_id { get; set; }
  39. public string photo_50 { get; set; }
  40. public string photo_100 { get; set; }
  41. public string photo_200 { get; set; }
  42. public List<Attachment> attachments { get; set; }
  43. }
  44. public class Item
  45. {
  46. public int unread { get; set; }
  47. public Message message { get; set; }
  48. }
  49. public class Response
  50. {
  51. public int count { get; set; }
  52. public int unread_dialogs { get; set; }
  53. public List<Item> items { get; set; }
  54. }
  55. public class RootObject
  56. {
  57. public Response response { get; set; }
  58. }
Заранее спасибо за ответ!

Решение задачи: «.NET 4.x [Newtonsoft.Json] Парсинг JSON-ответа»

textual
Листинг программы
  1. public class PushSettings
  2. {
  3.     public int sound { get; set; }
  4.     public int disabled_until { get; set; }
  5. }
  6.  
  7. public class Photo
  8. {
  9.     public int id { get; set; }
  10.     public int album_id { get; set; }
  11.     public int owner_id { get; set; }
  12.     public string photo_75 { get; set; }
  13.     public string photo_130 { get; set; }
  14.     public string photo_604 { get; set; }
  15.     public int width { get; set; }
  16.     public int height { get; set; }
  17.     public string text { get; set; }
  18.     public int date { get; set; }
  19.     public string access_key { get; set; }
  20. }
  21.  
  22. public class Attachment
  23. {
  24.     public string type { get; set; }
  25.     public Photo photo { get; set; }
  26. }
  27.  
  28. public class Message
  29. {
  30.     public int id { get; set; }
  31.     public int date { get; set; }
  32.     public int @out { get; set; }
  33.     public int user_id { get; set; }
  34.     public int read_state { get; set; }
  35.     public string title { get; set; }
  36.     public string body { get; set; }
  37.     public int chat_id { get; set; }
  38.     public List<int> chat_active { get; set; }
  39.     public PushSettings push_settings { get; set; }
  40.     public int users_count { get; set; }
  41.     public int admin_id { get; set; }
  42.     public string photo_50 { get; set; }
  43.     public string photo_100 { get; set; }
  44.     public string photo_200 { get; set; }
  45.     public List<Attachment> attachments { get; set; }
  46. }
  47.  
  48. public class Item
  49. {
  50.     public int unread { get; set; }
  51.     public Message message { get; set; }
  52. }
  53.  
  54. public class Response
  55. {
  56.     public int count { get; set; }
  57.     public int unread_dialogs { get; set; }
  58.     public List<Item> items { get; set; }
  59. }
  60.  
  61. public class RootObject
  62. {
  63.     public Response response { get; set; }
  64. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.091 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут