2

I get json data from a website. I then save this data to the .txt file.

{
  m: [
    {
      d: '08.01.2015',
      m: [
        [
          2875678,
          'id1',
          77,
          'id2',
          '100',
          0,
          '21:20',
          '',
          '',
          '3',
          '',
          '1',
          '-',
          '1.09',
          '2.06',
          '2.05',
          '1.40',
          '92334',
          {
            tId: 92334
          },
          '\u0130S1',
          '14',
          '20'
        ]
      ]
    }
  ]
}

The contents of the .txt file are as above.I want to make this .txt file a array.But when using json_decode the result is blank.

$file= fopen("test.txt", "r");
if( $file == false ) {
    echo ( "error!" );
    exit();
}else {
    while (!feof($file)) {
    $read = fgets($file);
    $decode = json_decode($read, true);
    print_r($decode);
    } 
}
fclose($file);

How can I convert it into an array? EDİT: data saved with this code.

$data = cURL($url);
$urlHandle     = _fwrite($data);
$datam        = $data;
$data        = json_decode($datam,true);

function _fwrite($data1)
{
    $file    = fopen('test.txt','w');
    fwrite($file,$data1);
    fclose($file);
  }
14
  • You may refer stackoverflow.com/questions/8858848/… Commented Feb 18, 2018 at 11:44
  • The thing is that's not JSON but a Javascript object Commented Feb 18, 2018 at 11:47
  • The problem is the JSON String is not valid, put it through jsonlint.com Commented Feb 18, 2018 at 11:49
  • @Borjante, What should I do for this? Commented Feb 18, 2018 at 11:53
  • Look up json_last_error_msg(); and echo json_last_error(); Commented Feb 18, 2018 at 11:53

1 Answer 1

0

This is your band-aid solution. This works on your provided sample input. It will work ...until it doesn't because of anomalies that are possible but not included in your post.

You need to take the time to isolate where and why your input data is malformed. Good applications are built from strong/stable foundations -- yours is not yet able to enjoy such stability.

Code: (Demo)

$badjson=<<<BADJSON
{
  m: [
    {
      d: '08.01.2015',
      m: [
        [
          2875678,
          'id1',
          77,
          'id2',
          '100',
          0,
          '21:20',
          '',
          '',
          '3',
          '',
          '1',
          '-',
          '1.09',
          '2.06',
          '2.05',
          '1.40',
          '92334',
          {
            tId: 92334
          },
          '\u0130S1',
          '14',
          '20'
        ]
      ]
    }
  ]
}
BADJSON;

var_export(json_decode(preg_replace(["~^\s+\K\w+(?=:)|^\s+\K\d+(?=,?\s*$)~m","~'~"],['"$0"','"'],$badjson),true));

Output:

array (
  'm' => 
  array (
    0 => 
    array (
      'd' => '08.01.2015',
      'm' => 
      array (
        0 => 
        array (
          0 => '2875678',
          1 => 'id1',
          2 => '77',
          3 => 'id2',
          4 => '100',
          5 => '0',
          6 => '21:20',
          7 => '',
          8 => '',
          9 => '3',
          10 => '',
          11 => '1',
          12 => '-',
          13 => '1.09',
          14 => '2.06',
          15 => '2.05',
          16 => '1.40',
          17 => '92334',
          18 => 
          array (
            'tId' => 92334,
          ),
          19 => 'İS1',
          20 => '14',
          21 => '20',
        ),
      ),
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.