Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Dekodowanie json
adamoski11
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 15.03.2015

Ostrzeżenie: (0%)
-----


Witam,

mam problem z zdekodowaniem json.

  1. object(stdClass)#1 (2) { ["meta"]=> object(stdClass)#2 (1) { ["code"]=> int(200) } ["data"]=> object(stdClass)#3 (7) { ["username"]=> string(10) "username" ["bio"]=> string(96) "biography" ["website"]=> string(21) "website_url" ["profile_picture"]=> string(106) "user_thumbnail" ["full_name"]=> string(11) "user_full_name" ["counts"]=> object(stdClass)#4 (3) { ["media"]=> int(596) ["followed_by"]=> int(195699) ["follows"]=> int(569) } ["id"]=> string(7) "7638875" } }


Mam taki kod, niestety zwraca on puste wartości, kompletnie nic, bez żadnych błędów. Jak pozmieniać poniższy kod żeby strona wyświetlała mi wartości ["data"]?

  1.  
  2. function getjson($userid)
  3. {
  4.  
  5. $url = "link_url";
  6. $get = file_get_contents($url);
  7. $json = json_decode($get);
  8. var_dump($json);
  9.  
  10.  
  11. foreach($json->data as $user)
  12. {
  13.  
  14. echo ''.$user->username.'';
  15.  
  16.  
  17. }}
  18.  
  19. echo getjson(12340);
  20.  

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
KsaR
post
Post #2





Grupa: Zarejestrowani
Postów: 520
Pomógł: 102
Dołączył: 15.07.2014
Skąd: NULL

Ostrzeżenie: (0%)
-----


Cytat(adamoski11 @ 7.06.2015, 22:27:27 ) *
Witam,

mam problem z zdekodowaniem json.

  1. object(stdClass)#1 (2) { ["meta"]=> object(stdClass)#2 (1) { ["code"]=> int(200) } ["data"]=> object(stdClass)#3 (7) { ["username"]=> string(10) "username" ["bio"]=> string(96) "biography" ["website"]=> string(21) "website_url" ["profile_picture"]=> string(106) "user_thumbnail" ["full_name"]=> string(11) "user_full_name" ["counts"]=> object(stdClass)#4 (3) { ["media"]=> int(596) ["followed_by"]=> int(195699) ["follows"]=> int(569) } ["id"]=> string(7) "7638875" } }


Mam taki kod, niestety zwraca on puste wartości, kompletnie nic, bez żadnych błędów. Jak pozmieniać poniższy kod żeby strona wyświetlała mi wartości ["data"]?

Zrób lepiej jesli mozesz na tablice tongue.gif

  1. $json=json_decode(COS,true);
  2. print_r($json['data']);
  3.  
  4. // ewentualnie potestuj inaczej:
  5. #1
  6. foreach($json->data['username'] as $user)
  7. {
  8. echo ''.$user.'';
  9. }
  10. # 2
  11. foreach($json->data->username as $user)
  12. {
  13. echo ''.$user.'';
  14.  
  15. }


Ten post edytował Ksar 7.06.2015, 21:49:45


--------------------
Go to the top of the page
+Quote Post
adamoski11
post
Post #3





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 15.03.2015

Ostrzeżenie: (0%)
-----


po zmianie na true, wyskakuje błąd:

"Warning: Invalid argument supplied for foreach()"
Go to the top of the page
+Quote Post
KsaR
post
Post #4





Grupa: Zarejestrowani
Postów: 520
Pomógł: 102
Dołączył: 15.07.2014
Skąd: NULL

Ostrzeżenie: (0%)
-----


Cytat(adamoski11 @ 7.06.2015, 22:52:06 ) *
po zmianie na true, wyskakuje błąd:

"Warning: Invalid argument supplied for foreach()"

Hm bo domyslnie zwraca obiekt stdClass.
Gdy dodasz w drugim argumencie true zwraca już tablice a nie obiekt.

Spróbuj te 2 przykłady ktore dalem w poprzednim poscie (bez "true").
Jak dalej nie dziala to pokaz tablice (tak jak nizej).

  1. $json = json_decode(cos,true);
  2. print_r($json);


Ten post edytował Ksar 7.06.2015, 21:56:30


--------------------
Go to the top of the page
+Quote Post
adamoski11
post
Post #5





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 15.03.2015

Ostrzeżenie: (0%)
-----


w dalszym ciągu nic,

  1. Array ( [meta] => Array ( [code] => 200 ) [data] => Array ( [username] => ... [bio] => ... [website] => ... [profile_picture] => ... [full_name] => ... [counts] => Array ( [media] => ... [followed_by] => ... [follows] => ... ) [id] => ... ) )
Go to the top of the page
+Quote Post
Damonsson
post
Post #6





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Przecież to obiekt, po Twoim json_decode, więc na co Ci foreach?

Zamień to:
  1. foreach($json->data as $user)
  2. {
  3.  
  4. echo ''.$user->username.'';
  5.  
  6.  
  7. }


na

  1. echo $json->data->username;


Ten post edytował Damonsson 7.06.2015, 23:00:09
Go to the top of the page
+Quote Post
com
post
Post #7





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

Ostrzeżenie: (0%)
-----


Ksar jak miał zamienić na tablice to nie ma przecież obiektu czyli nie -> a tablice smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 06:12