Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: JSON
Forum PHP.pl > Forum > PHP
szczawik
Witam jak wyświetlić wartości z tego linka?
http://api.jamendo.com/get2/id+name+url+im...ratingweek_desc
wookieb
json_decode
szczawik
a jak wyświetlić dowolny element?

  1. {"images":{"image":{"title":"","url":"http:\/\/www.last.fm\/music\/Ewa+Farna\/+images\/57591921","dateadded":"Thu, 20 Jan 2011 15:56:58","format":"png","owner":{"name":"maddie_XD","url":"http:\/\/www.last.fm\/user\/maddie_XD","@attr":{"type":"user"}},"sizes":{"size":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/_\/57591921\/Ewa+Farna.png","name":"original","width":"248","height":"371"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/57591921.png","name":"large","width":"126","height":"188"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126s\/57591921.png","name":"largesquare","width":"126","height":"126"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/57591921.png","name":"medium","width":"64","height":"96"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/57591921.png","name":"small","width":"34","height":"51"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/57591921.png","name":"extralarge","width":"252","height":"377"}]},"votes":{"thumbsup":"38","thumbsdown":"3"}},"@attr":{"artist":"Ewa Farna","page":"1","perPage":"1","totalPages":"224","total":"224"}}}


jak wyświetlić tylko
http:\/\/userserve-ak.last.fm\/serve\/_\/57591921\/Ewa+Farna.png
?
Ociu
  1. $jsonObj = json_decode('{"images":{"image":{"title":"","url":"http:\/\/www.last.fm\/music\/Ewa+Farna\/+images\/57591921","dateadded":"Thu, 20 Jan 2011 15:56:58","format":"png","owner":{"name":"maddie_XD","url":"http:\/\/www.last.fm\/user\/maddie_XD","@attr":{"type":"user"}},"sizes":{"size":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/_\/57591921\/Ewa+Farna.png","name":"original","width":"248","height":"371"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/57591921.png","name":"large","width":"126","height":"188"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126s\/57591921.png","name":"largesquare","width":"126","height":"126"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/57591921.png","name":"medium","width":"64","height":"96"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/57591921.png","name":"small","width":"34","height":"51"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/57591921.png","name":"extralarge","width":"252","height":"377"}]},"votes":{"thumbsup":"38","thumbsdown":"3"}},"@attr":{"artist":"Ewa Farna","page":"1","perPage":"1","totalPages":"224","total":"224"}}} ');
  2. var_dump($jsonObj);
szczawik
otrzymałem
  1. object(stdClass)#1 (1) { ["images"]=> object(stdClass)#2 (2) { ["image"]=> object(stdClass)#3 (7) { ["title"]=> string(0) "" ["url"]=> string(51) "http://www.last.fm/music/Ewa+Farna/+images/57591921" ["dateadded"]=> string(25) "Thu, 20 Jan 2011 15:56:58" ["format"]=> string(3) "png" ["owner"]=> object(stdClass)#4 (3) { ["name"]=> string(9) "maddie_XD" ["url"]=> string(33) "http://www.last.fm/user/maddie_XD" ["@attr"]=> object(stdClass)#5 (1) { ["type"]=> string(4) "user" } } ["sizes"]=> object(stdClass)#6 (1) { ["size"]=> array(6) { [0]=> object(stdClass)#7 (4) { ["#text"]=> string(58) "http://userserve-ak.last.fm/serve/_/57591921/Ewa+Farna.png" ["name"]=> string(8) "original" ["width"]=> string(3) "248" ["height"]=> string(3) "371" } [1]=> object(stdClass)#8 (4) { ["#text"]=> string(50) "http://userserve-ak.last.fm/serve/126/57591921.png" ["name"]=> string(5) "large" ["width"]=> string(3) "126" ["height"]=> string(3) "188" } [2]=> object(stdClass)#9 (4) { ["#text"]=> string(51) "http://userserve-ak.last.fm/serve/126s/57591921.png" ["name"]=> string(11) "largesquare" ["width"]=> string(3) "126" ["height"]=> string(3) "126" } [3]=> object(stdClass)#10 (4) { ["#text"]=> string(49) "http://userserve-ak.last.fm/serve/64/57591921.png" ["name"]=> string(6) "medium" ["width"]=> string(2) "64" ["height"]=> string(2) "96" } [4]=> object(stdClass)#11 (4) { ["#text"]=> string(49) "http://userserve-ak.last.fm/serve/34/57591921.png" ["name"]=> string(5) "small" ["width"]=> string(2) "34" ["height"]=> string(2) "51" } [5]=> object(stdClass)#12 (4) { ["#text"]=> string(50) "http://userserve-ak.last.fm/serve/252/57591921.png" ["name"]=> string(10) "extralarge" ["width"]=> string(3) "252" ["height"]=> string(3) "377" } } } ["votes"]=> object(stdClass)#13 (2) { ["thumbsup"]=> string(2) "38" ["thumbsdown"]=> string(1) "3" } } ["@attr"]=> object(stdClass)#14 (5) { ["artist"]=> string(9) "Ewa Farna" ["page"]=> string(1) "1" ["perPage"]=> string(1) "1" ["totalPages"]=> string(3) "224" ["total"]=> string(3) "224" } } }


Co z tym dalej zrobić?
Ociu
Następnie weź kurs php i poczytaj co z tym zrobić.
Dostałeś praktycznie gotowca!

Edit:
Jeżeli nie znasz obiektów, to wynik dostaniesz w postaci tablicy:
  1. $jsonObj = json_decode('{"images":{"image":{"title":"","url":"http:\/\/www.last.fm\/music\/Ewa+Farna\/+images\/57591921","dateadded":"Thu, 20 Jan 2011 15:56:58","format":"png","owner":{"name":"maddie_XD","url":"http:\/\/www.last.fm\/user\/maddie_XD","@attr":{"type":"user"}},"sizes":{"size":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/_\/57591921\/Ewa+Farna.png","name":"original","width":"248","height":"371"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/57591921.png","name":"large","width":"126","height":"188"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126s\/57591921.png","name":"largesquare","width":"126","height":"126"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/57591921.png","name":"medium","width":"64","height":"96"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/57591921.png","name":"small","width":"34","height":"51"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/57591921.png","name":"extralarge","width":"252","height":"377"}]},"votes":{"thumbsup":"38","thumbsdown":"3"}},"@attr":{"artist":"Ewa Farna","page":"1","perPage":"1","totalPages":"224","total":"224"}}} ', true);
  2. var_dump($jsonObj);
szczawik
Wyświetliłem w tablicy
  1. $array = (array)json_decode($homepage);

efekt:
  1. (
  2. [images] => stdClass Object
  3. (
  4. [image] => stdClass Object
  5. (
  6. [title] =>
  7. [url] => <a href="http://www.last.fm/music/Ewa+Farna/+images/57591921" target="_blank">http://www.last.fm/music/Ewa+Farna/+images/57591921</a>
  8. [dateadded] => Thu, 20 Jan 2011 15:56:58
  9. [format] => png
  10. [owner] => stdClass Object
  11. (
  12. [name] => maddie_XD
  13. [url] => <a href="http://www.last.fm/user/maddie_XD" target="_blank">http://www.last.fm/user/maddie_XD</a>
  14. [@attr] => stdClass Object
  15. (
  16. [type] => user
  17. )
  18.  
  19. )
  20.  
  21. [sizes] => stdClass Object
  22. (
  23. [size] => Array
  24. (
  25. [0] => stdClass Object
  26. (
  27. [#text] => <a href="http://userserve-ak.last.fm/serve/_/57591921/Ewa+Farna.png" target="_blank">http://userserve-ak.last.fm/serve/_/57591921/Ewa+Farna.png</a>
  28. [name] => original
  29. [width] => 248
  30. [height] => 371
  31. )
  32.  
  33. [1] => stdClass Object
  34. (
  35. [#text] => <a href="http://userserve-ak.last.fm/serve/126/57591921.png" target="_blank">http://userserve-ak.last.fm/serve/126/57591921.png</a>
  36. [name] => large
  37. [width] => 126
  38. [height] => 188
  39. )
  40.  
  41. [2] => stdClass Object
  42. (
  43. [#text] => <a href="http://userserve-ak.last.fm/serve/126s/57591921.png" target="_blank">http://userserve-ak.last.fm/serve/126s/57591921.png</a>
  44. [name] => largesquare
  45. [width] => 126
  46. [height] => 126
  47. )
  48.  
  49. [3] => stdClass Object
  50. (
  51. [#text] => <a href="http://userserve-ak.last.fm/serve/64/57591921.png" target="_blank">http://userserve-ak.last.fm/serve/64/57591921.png</a>
  52. [name] => medium
  53. [width] => 64
  54. [height] => 96
  55. )
  56.  
  57. [4] => stdClass Object
  58. (
  59. [#text] => <a href="http://userserve-ak.last.fm/serve/34/57591921.png" target="_blank">http://userserve-ak.last.fm/serve/34/57591921.png</a>
  60. [name] => small
  61. [width] => 34
  62. [height] => 51
  63. )
  64.  
  65. [5] => stdClass Object
  66. (
  67. [#text] => <a href="http://userserve-ak.last.fm/serve/252/57591921.png" target="_blank">http://userserve-ak.last.fm/serve/252/57591921.png</a>
  68. [name] => extralarge
  69. [width] => 252
  70. [height] => 377
  71. )
  72.  
  73. )
  74.  
  75. )
  76.  
  77. [votes] => stdClass Object
  78. (
  79. [thumbsup] => 38
  80. [thumbsdown] => 3
  81. )
  82.  
  83. )
  84.  
  85. [@attr] => stdClass Object
  86. (
  87. [artist] => Ewa Farna
  88. [page] => 1
  89. [perPage] => 1
  90. [totalPages] => 224
  91. [total] => 224
  92. )
  93.  
  94. )
  95.  
  96. )

teraz próbuje wyciągnąć
  1. $text = '#text';
  2. print $array[0]->images->image->sizes->size[0]->$text;


Za mało wiedzy mam biggrin.gif
W manualu tylko proste przykłady są
Ociu
Zobacz co Ci pokaże
  1. $array = json_decode($homepage, true);
  2. var_dump($array);
szczawik
otrzymałem:
  1. array(1) {
  2. ["images"]=>
  3. array(2) {
  4. ["image"]=>
  5. array(7) {
  6. ["title"]=>
  7. string(0) ""
  8. ["url"]=>
  9. string(51) "http://www.last.fm/music/Ewa+Farna/+images/57591921"
  10. ["dateadded"]=>
  11. string(25) "Thu, 20 Jan 2011 15:56:58"
  12. ["format"]=>
  13. string(3) "png"
  14. ["owner"]=>
  15. array(3) {
  16. ["name"]=>
  17. string(9) "maddie_XD"
  18. ["url"]=>
  19. string(33) "http://www.last.fm/user/maddie_XD"
  20. ["@attr"]=>
  21. array(1) {
  22. ["type"]=>
  23. string(4) "user"
  24. }
  25. }
  26. ["sizes"]=>
  27. array(1) {
  28. ["size"]=>
  29. array(6) {
  30. [0]=>
  31. array(4) {
  32. ["#text"]=>
  33. string(58) "http://userserve-ak.last.fm/serve/_/57591921/Ewa+Farna.png"
  34. ["name"]=>
  35. string(8) "original"
  36. ["width"]=>
  37. string(3) "248"
  38. ["height"]=>
  39. string(3) "371"
  40. }
  41. [1]=>
  42. array(4) {
  43. ["#text"]=>
  44. string(50) "http://userserve-ak.last.fm/serve/126/57591921.png"
  45. ["name"]=>
  46. string(5) "large"
  47. ["width"]=>
  48. string(3) "126"
  49. ["height"]=>
  50. string(3) "188"
  51. }
  52. [2]=>
  53. array(4) {
  54. ["#text"]=>
  55. string(51) "http://userserve-ak.last.fm/serve/126s/57591921.png"
  56. ["name"]=>
  57. string(11) "largesquare"
  58. ["width"]=>
  59. string(3) "126"
  60. ["height"]=>
  61. string(3) "126"
  62. }
  63. [3]=>
  64. array(4) {
  65. ["#text"]=>
  66. string(49) "http://userserve-ak.last.fm/serve/64/57591921.png"
  67. ["name"]=>
  68. string(6) "medium"
  69. ["width"]=>
  70. string(2) "64"
  71. ["height"]=>
  72. string(2) "96"
  73. }
  74. [4]=>
  75. array(4) {
  76. ["#text"]=>
  77. string(49) "http://userserve-ak.last.fm/serve/34/57591921.png"
  78. ["name"]=>
  79. string(5) "small"
  80. ["width"]=>
  81. string(2) "34"
  82. ["height"]=>
  83. string(2) "51"
  84. }
  85. [5]=>
  86. array(4) {
  87. ["#text"]=>
  88. string(50) "http://userserve-ak.last.fm/serve/252/57591921.png"
  89. ["name"]=>
  90. string(10) "extralarge"
  91. ["width"]=>
  92. string(3) "252"
  93. ["height"]=>
  94. string(3) "377"
  95. }
  96. }
  97. }
  98. ["votes"]=>
  99. array(2) {
  100. ["thumbsup"]=>
  101. string(2) "38"
  102. ["thumbsdown"]=>
  103. string(1) "3"
  104. }
  105. }
  106. ["@attr"]=>
  107. array(5) {
  108. ["artist"]=>
  109. string(9) "Ewa Farna"
  110. ["page"]=>
  111. string(1) "1"
  112. ["perPage"]=>
  113. string(1) "1"
  114. ["totalPages"]=>
  115. string(3) "224"
  116. ["total"]=>
  117. string(3) "224"
  118. }
  119. }
  120. }
Ociu
Zaczyna mi się kończyć cierpliwość.
To jest tablica asocjacyjna. To co robiłeś na podstawie obiektów zrób na podstawie tablic asocjacyjnych.
szczawik
do mnie trzeba mieć anielską biggrin.gif

dałem coś takiego
$tablica['sizes']['size'][0]['#text'];
i pusto ;/
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.