Excelption #1

Open
opened 2019-12-23 13:15:12 +03:00 by Ghost · 1 comment

Hi I keep getting and error when I parse data in the following format, sadly this comes from a 3rd party API so I cant change it

{
  "Success": true,
  "Status": "Ok",
  "Data": [
    {
      "timeLive": "43",
      "halfTimeScore": "0 - 0",
      "fullTimeScore": null,
      "firstTeamToScore": null,
      "homeTeamInfo": {
        "homeTeam": "Indonesia",
        "homeGoals": "0",
        "homeGoalsHalfTime": "0",
        "homeCorners": "0",
        "homeYellowCards": "0",
        "homeRedCards": "0",
        "homeShotsOnTarget": "0",
        "homeShotsOffTarget": "0",
        "homeAttacks": "0",
        "homeDangerousAttack": "0",
        "homePossession": "0",
        "homeFouls": 0,
        "avgGoalsHome": null,
        "teamID": "44CB1",
        "homeTeamForm": null

I dont think it likes the structure with the DATA at the top but im unsure how I can get round it

The error is
PHP Fatal error: Uncaught exception 'JSONStreamException' with message 'unexpected token at offset 0:

Hi I keep getting and error when I parse data in the following format, sadly this comes from a 3rd party API so I cant change it ``` { "Success": true, "Status": "Ok", "Data": [ { "timeLive": "43", "halfTimeScore": "0 - 0", "fullTimeScore": null, "firstTeamToScore": null, "homeTeamInfo": { "homeTeam": "Indonesia", "homeGoals": "0", "homeGoalsHalfTime": "0", "homeCorners": "0", "homeYellowCards": "0", "homeRedCards": "0", "homeShotsOnTarget": "0", "homeShotsOffTarget": "0", "homeAttacks": "0", "homeDangerousAttack": "0", "homePossession": "0", "homeFouls": 0, "avgGoalsHome": null, "teamID": "44CB1", "homeTeamForm": null ``` I dont think it likes the structure with the DATA at the top but im unsure how I can get round it The error is PHP Fatal error: Uncaught exception 'JSONStreamException' with message 'unexpected token at offset 0:

Hi. It's interesting that you've found this repo :)

I suspect you copy-pasted the example with enterArray() at the upper level, which is incorrect for objects. Call enterObject() to enter the top-level object, like:

<?php

require_once './JSONStream.php';

$fp = fopen('test.json', 'r');
$json = new JSONStream(function() use($fp) { return fread($fp, 262144); });
$json->enterObject();
while ($json->readValue($k))
{
    $json->readValue($v);
    var_dump([$k, $v]);
}
$json->exitObject();
fclose($fp);

You'll probably want to make multiple enterXXX() calls. For example, call enterArray() in the above example when $k is equal to "Data"...

Hi. It's interesting that you've found this repo :) I suspect you copy-pasted the example with enterArray() at the upper level, which is incorrect for objects. Call enterObject() to enter the top-level object, like: ```php <?php require_once './JSONStream.php'; $fp = fopen('test.json', 'r'); $json = new JSONStream(function() use($fp) { return fread($fp, 262144); }); $json->enterObject(); while ($json->readValue($k)) { $json->readValue($v); var_dump([$k, $v]); } $json->exitObject(); fclose($fp); ``` You'll probably want to make multiple enterXXX() calls. For example, call enterArray() in the above example when $k is equal to "Data"...
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: vitalif/fast-json-stream#1
There is no content yet.