2

I have this JSON string:

 {
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}

But it looks like it's not correct (JSONLint tells me) so PHP's json_decode()can't decode it. There's any way to separate the two JSON arrays into two strings (or into how much string the arrays are) for making json_decode decode them?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
pmerino
  • 5,900
  • 11
  • 57
  • 76

3 Answers3

2

Assuming your intention is to have an array of two elements, your JSON should look like:

[
    {
        "name": "test task1",
        "desc": "test desc1",
        "id": "1"
    },{
        "name": "test task1aaaa",
        "desc": "test desc1",
        "id": "2"
    }
]
karim79
  • 339,989
  • 67
  • 413
  • 406
0

the most straightforward

$str = ' {
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}';

var_dump(json_decode('['.str_replace('}{','},{',$str).']'));
k102
  • 7,861
  • 7
  • 49
  • 69
0
<?php
$str='{
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}';

$arrays = explode("{", $str);
foreach($arrays as &$arr) $arr='{'.$arr;

//decode
foreach ($arrays as $arr) print_r(json_decode($arr,true));
OZ_
  • 12,492
  • 7
  • 50
  • 68