mardi 29 août 2017

values returned by the server invalid json

I'm using json_decode to parse a result returned by the server, the code below used to work fine with one unit, but recently there're new "units": ["ug/m3""mg/m3"] and they are not separated by comma.

  $client = new Client(); //GuzzleHttp\Client
  $result = $client->get($url);
  $body = (string)$result->getBody(); 

   $area = json_decode($body, true);

    foreach($area as $v)
       {
          $arr[$v['key']] = $v['key'];
       }

  }

Invalid argument supplied for foreach()

'body' => '[{ "key": "0001", "desc": "SO2", "units": ["ppb""ug/m3"] },
{ "key": "0003", "desc": "NO2", "units": ["ppb""ug/m3"] },
{ "key": "0004", "desc": "CO", "units": ["ppm""mg/m3""ug/m3"] },
{ "key": "0008", "desc": "O3", "units": ["ppb""ug/m3"] },
{ "key": "PM10", "desc": "PM10", "units": ["ug/m3""mg/m3"] },
{ "key": "PM25", "desc": "PM25", "units": ["ug/m3""mg/m3"] }]', 'area' => null)  



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire