Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]JSON foreach
rybosom
post
Post #1





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


mam następujący JSON response
Cytat
{"transactionId":0,"authorizationId":13,"accountNumber":"*ciach*","creditCardNumber":"*ciach*","icon":2,"iconDescription":"Nierozliczone","date":"2014-09-17T00:00:00.000","desc":"Pizzeria","subCategName":"Wyjścia i wydarzenia","amount":-37.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"*ciach*","creditCardNumber":"","icon":0,"iconDescription":"Przelew","date":"2014-09-17T00:00:00.000","desc":"PRZELEW REGULARNE OSZCZĘDZANIE","subCategName":"Regularne oszczędzanie","amount":-9.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"*ciach*","creditCardNumber":"","icon":0,"iconDescription":"Przelew","date":"2014-09-17T00:00:00.000","desc":"*ciach*","subCategName":"Prąd","amount":-60.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"*ciach*","creditCardNumber":"","icon":0,"iconDescription":"Przelew","date":"2014-09-16T00:00:00.000","desc":"PRZELEW REGULARNE OSZCZĘDZANIE","subCategName":"Regularne oszczędzanie","amount":-6.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"*ciach*","creditCardNumber":"","icon":0,"iconDescription":"Przelew","date":"2014-09-16T00:00:00.000","desc":"*ciach*","subCategName":"Wpływy - inne","amount":817.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"*ciach*","creditCardNumber":"*ciach*","icon":1,"iconDescription":"Płatność kartą","date":"2014-09-15T00:00:00.000","desc":"TESCO","subCategName":"Żywność i chemia domowa","amount":-*ciach*.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"*ciach*","creditCardNumber":"*ciach*","icon":4,"iconDescription":"Inna operacja","date":"2014-09-15T00:00:00.000","desc":"OPŁATA ZA DODATKOWĄ KARTĘ","subCategName":"Opłaty i odsetki","amount":-3.00,"currency":"PLN"}
przy pomocy
  1. $obj_historia = json_decode($json_historia);
  2. foreach ($obj_historia as $name => $value) {
  3. echo $date.' '.$desc.' '.$amount.' '.$currency;
  4. }
chcę odczytać wybrane rzeczy jednakże zwraca Invalid argument supplied for foreach()

gdzie w pętli zrobiłem błąd?

Ten post edytował rybosom 18.09.2014, 13:49:29
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 19)
Turson
post
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Przed pętlą zrób
  1. echo '<pre>'; var_dump($obj_historia); echo '</pre>';

sprawdz jak wyglada, albo wklej nam
Go to the top of the page
+Quote Post
rybosom
post
Post #3





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


o ja! wynik to NULL
Go to the top of the page
+Quote Post
Rochu
post
Post #4





Grupa: Zarejestrowani
Postów: 64
Pomógł: 14
Dołączył: 25.09.2010

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


Dzieje się tak z tego względu, że JSON response, który wkleiłeś jest błędny - dlatego też funkcja json_decode zwraca null.
Go to the top of the page
+Quote Post
rybosom
post
Post #5





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


tak jest błędny bo zawiera 7 tablic ale taki ja właśnie otrzymuję.

muszę chyba najpierw rozbić {},{},{},{},{},{},{} i zrobić pętlę w pętli, dobrze myślę?
Go to the top of the page
+Quote Post
Rochu
post
Post #6





Grupa: Zarejestrowani
Postów: 64
Pomógł: 14
Dołączył: 25.09.2010

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


ewentualnie "naprawiasz" dany JSON, by funkcja json_decode poprawnie go obsłużyła i wrzucasz go w pętlę, moim zdaniem bardziej optymalne
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


dodaj do JSON [ na początku i ] na końcu stringu. Zadziała (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
rybosom
post
Post #8





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


Cytat(Pyton_000 @ 18.09.2014, 13:58:54 ) *
dodaj do JSON [ na początku i ] na końcu stringu. Zadziała (IMG:style_emoticons/default/wink.gif)



przy

  1. $obj_historia = json_decode('['.$json_historia.']');
  2.  
  3. echo '<pre>'; var_dump($obj_historia); echo '</pre>';


też jest NULL
Go to the top of the page
+Quote Post
Pyton_000
post
Post #9





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Kod
"Żywność i chemia domowa","amount":-*ciach*.48,"currency":"PLN"},

->
Cytat
amount":-*ciach*.48,"currency":"PLN"}

Brakuje ""
Go to the top of the page
+Quote Post
rybosom
post
Post #10





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


faktycznie, teraz widzę że JSON response zawiera same błędy bo taka konstrukcja jest w każdej tablicy
Cytat
"icon":0,


"transactionId":0,


"authorizationId":13,


"amount":-3.00


czyli kapota bo nie mam wpływu na kształt otrzymanych danych

chyba że dorobię jakieś sprytne wyrażenie regularne

Ten post edytował rybosom 18.09.2014, 14:11:55
Go to the top of the page
+Quote Post
Pyton_000
post
Post #11





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ehh...

-3.00 zostanie poprawnie zinterpretowwa bo to liczba, ale już -ciach.48 to jes string.

Przez to że wstawiłes tam "ciach" rozsypałeś JSON

Ten post edytował Pyton_000 18.09.2014, 14:18:41
Go to the top of the page
+Quote Post
rybosom
post
Post #12





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


Cytat(Pyton_000 @ 18.09.2014, 15:18:04 ) *
Przez to że wstawiłes tam "ciach" rozsypałeś JSON
masz rację, poprawiłem JSON
  1. {"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}


i gdy wykonuję
  1. $pieces = explode("},{", $json_historia);
  2. echo '<pre>'; var_dump($pieces); echo '</pre>';
otrzymuję
  1. array(7) {
  2. [0]=>
  3. string(215) "{"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN""
  4. [1]=>
  5. string(216) ""transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN""
  6. [2]=>
  7. string(216) ""transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN""
  8. [3]=>
  9. string(216) ""transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN""
  10. [4]=>
  11. string(215) ""transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN""
  12. [5]=>
  13. string(215) ""transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN""
  14. [6]=>
  15. string(217) ""transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}"
  16. }


teraz muszę wymyślić jakaś sprytną pętlę aby wyświetlić tylko "date - desc - amount - currency"
Go to the top of the page
+Quote Post
Pyton_000
post
Post #13





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


  1. foreach($decoded_json as $json) {
  2. $small_json = json_decode($json);
  3. echo $small_json['date'];
  4. }


tak na szybko
Go to the top of the page
+Quote Post
rybosom
post
Post #14





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


  1. $json_historia = $row["historia"];
  2. $jj = explode("},{", $json_historia);
  3. $obj_historia = json_decode($jj);
  4. foreach($obj_historia as $json) {
  5. $small_json = json_decode($json);
  6. echo $small_json['date'];
  7. }


zwraca

Warning: json_decode() expects parameter 1 to be string, array given in j.php on line 45
Warning: Invalid argument supplied for foreach() in j.php on line 46
Go to the top of the page
+Quote Post
Pyton_000
post
Post #15





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ehhh....
i po cholere ta kombinacja ?

To co dałeś jako "poprawione" w poprzednim poscie umieść między [] i przepuść przez json_decode. Otrzymasz zdekodowanego jsona a nie jakieś zabawy :|

Czyli:

  1. $json = '[{"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}]';
  2.  
  3. $json = json_decode($json));
  4. var_dump(json_decode($json));
  5.  
  6. echo $json->date;


Ew. drugi parametr json_decode na true i masz same tablce

Ten post edytował Pyton_000 18.09.2014, 17:48:44
Go to the top of the page
+Quote Post
rybosom
post
Post #16





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


nie wiem czy dobrze zrozumiałem, dałem

  1. $json = '[{"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}]';
  2.  
  3. $json = json_decode($json);
  4. var_dump(json_decode($json), TRUE);
  5.  
  6. echo $json->date[0];


i dostałem

Cytat
Warning: json_decode() expects parameter 1 to be string, array given in j.php on line 5
NULL bool(true)
Go to the top of the page
+Quote Post
Turson
post
Post #17





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


A jak raz zdekodujesz?
Go to the top of the page
+Quote Post
rybosom
post
Post #18





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


  1. $json_historia = '[{"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}]';
  2.  
  3. $pieces = explode("},{", $json_historia);
  4. $json = json_decode($pieces);
  5.  
  6. echo $json->date;


daje to samo czyli Warning: json_decode() expects parameter 1 to be string

Ten post edytował rybosom 18.09.2014, 18:55:43
Go to the top of the page
+Quote Post
Pyton_000
post
Post #19





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ja już straciłem cierpliwość... Chciałbym napisać kilka niecenzuralnych słów ale powstrzymam się dla swojego dobra.

  1. $json = '[{"transactionId":0,"authorizationId":13,"accountNumber":"cut","creditCardNumber":"","icon":2,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.80,"currency":"PLN"},{"transactionId":6901,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.63,"currency":"PLN"},{"transactionId":6900,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-17T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.37,"currency":"PLN"},{"transactionId":6899,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.52,"currency":"PLN"},{"transactionId":6898,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":0,"iconDescription":"cut","date":"2014-09-16T00:00:00.000","desc":"cut","subCategName":"cut","amount":1.38,"currency":"PLN"},{"transactionId":51,"authorizationId":25,"accountNumber":"cut","creditCardNumber":"","icon":1,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-1.48,"currency":"PLN"},{"transactionId":6897,"authorizationId":0,"accountNumber":"cut","creditCardNumber":"","icon":4,"iconDescription":"cut","date":"2014-09-15T00:00:00.000","desc":"cut","subCategName":"cut","amount":-3.00,"currency":"PLN"}]';
  2.  
  3. $json = json_decode($json, true);
  4. echo '<pre>'; var_dump($json); die("\n".(__FILE__)." ".(__LINE__));
Go to the top of the page
+Quote Post
rybosom
post
Post #20





Grupa: Zarejestrowani
Postów: 82
Pomógł: 0
Dołączył: 6.09.2004

Ostrzeżenie: (20%)
X----


Cytat(Pyton_000 @ 18.09.2014, 20:07:48 ) *
Ja już straciłem cierpliwość...



cierpliwość się opłaciła (IMG:style_emoticons/default/wink.gif)

bardzo ładne tablice, czyli teraz foreach aby uzyskać date - desc - amount - currency

mam nadzieję że pójdzie łatwo

Ten post edytował rybosom 18.09.2014, 19:44:39
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 22.08.2025 - 14:17