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?

3 Answers 3

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"
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, just made it correct, was generating the array wrong :D
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).']'));

Comments

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));

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.