Guest User

Untitled

a guest
Jan 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. var xml = (function() {
  2. /*
  3.  
  4. <?xml version="1.0" encoding="UTF-8" ?>
  5.  
  6. <Results>
  7. <show>
  8. <showid>2930</showid>
  9. <name>Buffy the Vampire Slayer</name>
  10. <link>http://www.tvrage.com/Buffy_The_Vampire_Slayer</link>
  11. <country>US</country>
  12. <started>Mar/10/1997</started>
  13. <ended>May/20/2003</ended>
  14. <seasons>7</seasons>
  15. <status>Ended</status>
  16. <runtime>60</runtime>
  17. <classification>Scripted</classification>
  18. <genres><genre>Action</genre><genre>Adventure</genre><genre>Comedy</genre><genre>Drama</genre><genre>Horror/Supernatural</genre><genre>Mystery</genre><genre>Sci-Fi</genre></genres>
  19. <network country="US">UPN</network>
  20. <airtime>20:00</airtime>
  21. <airday>Tuesday</airday>
  22. <akas><aka country="SE">Buffy & vampyrerna</aka><aka country="DE">Buffy - Im Bann der Dämonen</aka><aka country="NO">Buffy - Vampyrenes skrekk</aka><aka country="HU">Buffy a vámpírok réme</aka><aka country="FR">Buffy Contre les Vampires</aka><aka country="IT">Buffy l'Ammazza Vampiri</aka><aka country="PL">Buffy postrach wampirów</aka><aka country="BR">Buffy, a Caça-Vampiros</aka><aka country="PT">Buffy, a Caçadora de Vampiros</aka><aka country="ES">Buffy, Cazavampiros</aka><aka country="HR">Buffy, ubojica vampira</aka><aka country="FI">Buffy, vampyyrintappaja</aka><aka country="EE">Vampiiritapja Buffy</aka><aka country="IS">Vampírubaninn Buffy</aka><aka country="RU">Баффи – истребительница вампиров</aka></akas>
  23. </show>
  24. <show>
  25. <showid>31192</showid>
  26. <name>Buffy the Vampire Slayer - Season Eight: Motion comics</name>
  27. <link>http://www.tvrage.com/shows/id-31192</link>
  28. <country>US</country>
  29. <started>Jul/19/2010</started>
  30. <ended>Nov/22/2010</ended>
  31. <seasons>1</seasons>
  32. <status>Canceled/Ended</status>
  33. <runtime>15</runtime>
  34. <classification>Animation</classification>
  35. <genres><genre>Animation General</genre><genre>Action</genre><genre>Adventure</genre><genre>Comedy</genre><genre>Drama</genre><genre>Horror/Supernatural</genre><genre>Sci-Fi</genre></genres>
  36. <network country="US">iTunes</network>
  37. <airtime>12:00</airtime>
  38. <airday>Tuesday</airday>
  39. </show>
  40. <show>
  41. <showid>2931</showid>
  42. <name>Buffy the Animated Series</name>
  43. <link>http://www.tvrage.com/Buffy_the_Animated_Series</link>
  44. <country>US</country>
  45. <started>2002</started>
  46. <ended></ended>
  47. <seasons>1</seasons>
  48. <status>Pilot Rejected</status>
  49. <runtime>4</runtime>
  50. <classification>Animation</classification>
  51. <genres><genre>Animation General</genre><genre>Action</genre><genre>Adventure</genre><genre>Horror/Supernatural</genre></genres>
  52. <network country="US">FOX</network>
  53. <airtime>12:00</airtime>
  54. <airday>Tuesday</airday>
  55. </show>
  56. </Results>
  57. */
  58. }).toString().split('n').slice(2, -2).join('n').trim();
  59.  
  60. var oParser = new DOMParser();
  61. var xml = oParser.parseFromString(xml, "text/xml");
  62.  
  63. // Changes XML to JSON
  64. function xmlToJson(xml) {
  65.  
  66. // Create the return object
  67. var obj = {};
  68.  
  69. if (xml.nodeType == 1) { // element
  70. // do attributes
  71. if (xml.attributes.length > 0) {
  72. //obj["attributes"] = {};
  73. for (var j = 0; j < xml.attributes.length; j++) {
  74. var attribute = xml.attributes.item(j);
  75. //obj["attributes"][attribute.nodeName] = attribute.nodeValue;
  76. obj['@' + attribute.nodeName] = attribute.nodeValue;
  77. }
  78. }
  79. } else if (xml.nodeType == 3) { // text
  80. obj = xml.nodeValue.trim(); // add trim here
  81. }
  82.  
  83. // do children
  84. if (xml.hasChildNodes()) {
  85. for (var i = 0; i < xml.childNodes.length; i++) {
  86. var item = xml.childNodes.item(i);
  87. var nodeName = item.nodeName;
  88. // console.debug('child',nodeName,item)
  89. if (typeof(obj[nodeName]) == "undefined") {
  90. var tmp = xmlToJson(item);
  91. if (tmp !== "") // if not empty string
  92. obj[nodeName] = tmp;
  93. } else {
  94. if (typeof(obj[nodeName].push) == "undefined") {
  95. var old = obj[nodeName];
  96. obj[nodeName] = [];
  97. obj[nodeName].push(old);
  98. }
  99. var tmp = xmlToJson(item);
  100. if (tmp !== "") // if not empty string
  101. obj[nodeName].push(tmp);
  102. }
  103. }
  104. }
  105. if (!Array.isArray(obj) && typeof obj == 'object') {
  106. var keys = Object.keys(obj);
  107. if (keys.length == 1 && keys[0] == '#text') return obj['#text'];
  108. if (keys.length === 0) return null;
  109. }
  110. return obj;
  111. }
  112.  
  113. var obj =xmlToJson(xml);
  114. console.clear();
  115. console.debug(obj);
  116. document.getElementById('pre').innerHTML=JSON.stringify(obj,null,' ');
  117.  
  118. public function getMarket($period, $page)
  119. {
  120. $items = [];
  121. $response = (array)@json_decode($this->request('GET', 'https://site.ru/json/my?duration=' . $period . '&page=' . $page)['body'], true);
  122.  
  123. foreach ($response as $key => $item) {
  124. $item['user'] = $this->getUser($item['userId']);
  125. $items[] = new MyItem($item);
  126. }
  127.  
  128. return $items;
  129. }
Add Comment
Please, Sign In to add comment