Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]wypisanie kolekcji JSON
kyku102
post 6.04.2023, 22:05:07
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 6.04.2023

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


Witam,

mam problem z wypisaniem kolejno vehicle_model i vehicle_price. Jak powinna wyglądać poprawnie pętla foreach w PHP, która wypisze te dane z kolekcji JSON takiej jak niżej?

{
  "permissions": {
    "isGroupOwner": false,
    "warehouse": true,
    "gates": true,
    "vehicles": true,
    "lombard": true,
    "leader": true
  },
  "warehouse": {
    "id": 1600,
    "warehouseID": 4,
    "groupID": 749,
    "expires": 1680940342,
    "warehouse": {
      "id": 4,
      "price_start": 670000,
      "price_rent": 65000,
      "size": 12,
      "minimum_level": 100,
      "vehicles": [
        {
          "id": 295898,
          "vehicle_model": 451,
          "default_vehicle_price": 1570500,
          "vehicle_price": 942300,
          "stats": "[[[1680789608,1115055],[1680793211,1303515],[1680796813,1319220],[168080041
7,1963125],[1680804002,1382040],[1680807605,1633320],[1680811207,942300]]]",
          "color": "[[123,123,0,0]]"
        },
        {
          "id": 296186,
          "vehicle_model": 562,
          "default_vehicle_price": 658240,
          "vehicle_price": 579251,
          "stats": "[[[1680800417,493680],[1680804002,427856],[1680807605,454185],[1680811207,5
79251]]]",
          "color": "[[116,1,0,0]]"
        },
        {
          "id": 296206,
          "vehicle_model": 434,
          "default_vehicle_price": 609840,
          "vehicle_price": 408592,
          "stats": "[[[1680804002,548856],[1680807605,390297],[1680811207,408592]]]",
          "color": "[[1,1,0,0]]"
        },
        {
          "id": 296241,
          "vehicle_model": 562,
          "default_vehicle_price": 658240,
          "vehicle_price": 572668,
          "stats": "[[[1680811207,572668]]]",
          "color": "[[11,1,0,0]]"
        },
        {
          "id": 296254,
          "vehicle_model": 562,
          "default_vehicle_price": 658240,
          "vehicle_price": 230384,
          "stats": "[[]]",
          "color": "[[113,1,0,0]]"
        }
      ]
    }
  }
}
Go to the top of the page
+Quote Post
Tomplus
post 7.04.2023, 05:22:43
Post #2





Grupa: Zarejestrowani
Postów: 1 838
Pomógł: 226
Dołączył: 20.03.2005
Skąd: Będzin

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


  1. foreach($json->warehouse->warehouse['vehicles'] as $vehicles) {
  2.  
  3. echo "<br>Pojazd: ".$vehicles->vehicle_model.", cena: ".$vehicles->vehicle_price;
  4.  
  5. }


Musisz wiedzieć, że klamerki {} determinują JSON jako obiekt, a nawiasy kwadratowe [] jako tablicę.


zresztą użyj var_dump($json) i będziesz miał wszystko co potrzebujesz,
Go to the top of the page
+Quote Post
kyku102
post 7.04.2023, 07:53:56
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 6.04.2023

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


Cytat(Tomplus @ 7.04.2023, 06:22:43 ) *
  1. foreach($json->warehouse->warehouse['vehicles'] as $vehicles) {
  2.  
  3. echo "<br>Pojazd: ".$vehicles->vehicle_model.", cena: ".$vehicles->vehicle_price;
  4.  
  5. }


Musisz wiedzieć, że klamerki {} determinują JSON jako obiekt, a nawiasy kwadratowe [] jako tablicę.


zresztą użyj var_dump($json) i będziesz miał wszystko co potrzebujesz,

To co zaproponowałeś nic mi nie wypisuje właśnie ;/
Go to the top of the page
+Quote Post
viking
post 7.04.2023, 08:11:06
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Zrób sobie json_decode($tenString, true, 512, JSON_THROW_ON_ERROR); na początek.
https://jsonformatter.curiousconcept.com/# Invalid character "stats":"[[[1680789608,1115055],[1680793211,1303515],[1680796813,1319220],[168080041
7,1963125],[1680804002,1382040],[1680807605,1633320],[1680811207,942300]]]",[Code 180, Structure 93]

Ten post edytował viking 7.04.2023, 08:13:51


--------------------
Go to the top of the page
+Quote Post
kyku102
post 7.04.2023, 10:01:28
Post #5





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 6.04.2023

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


Cytat(viking @ 7.04.2023, 09:11:06 ) *
Zrób sobie json_decode($tenString, true, 512, JSON_THROW_ON_ERROR); na początek.
https://jsonformatter.curiousconcept.com/# Invalid character "stats":"[[[1680789608,1115055],[1680793211,1303515],[1680796813,1319220],[168080041
7,1963125],[1680804002,1382040],[1680807605,1633320],[1680811207,942300]]]",[Code 180, Structure 93]

Dalej nic - kolekcje pobieram przez curl - tak to u mnie wygląda:

  1. $token = "xxxxx";
  2. $groupid = 749;
  3.  
  4. $curl = curl_init("https://ucp.paradise-rpg.pl/api/group/$groupid/warehouses");
  5.  
  6. $headers = [
  7. "Content-Type:application/json",
  8. "Authorization: Bearer $token",
  9. "user-agent: Chrome/79"
  10. ];
  11.  
  12. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  13. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  14.  
  15. $result = curl_exec($curl);
  16. curl_close($curl);
  17.  
  18. $json = json_decode($result, true, 512, JSON_THROW_ON_ERROR);
  19.  
  20. foreach($json->warehouse->warehouse['vehicles'] as $vehicles) {
  21. echo "<br>Pojazd: ".$vehicles->vehicle_model.", cena: ".$vehicles->vehicle_price;
  22. }


Ten post edytował kyku102 7.04.2023, 10:41:56
Go to the top of the page
+Quote Post
viking
post 7.04.2023, 10:25:05
Post #6





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Działa, json ze źródła jest poprawny:
  1. $data = json_decode($x, false, 512, JSON_THROW_ON_ERROR);
  2.  
  3. foreach($data->warehouse->warehouse->vehicles as $vehicles) {
  4. echo "<br>Pojazd: ".$vehicles->vehicle_model.", cena: ".$vehicles->vehicle_price;
  5. }


Możesz też zmienić drugi klucz na true wtedy będziesz miał $data jako tablicę.

Ukryj ten klucz jeśli jest jakiś prywatny.

Ten post edytował viking 7.04.2023, 10:26:03


--------------------
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 Wersja Lo-Fi Aktualny czas: 27.04.2024 - 18:40