Skip to content
Advertisement

JSON.aprse() error when using php json_encode()

  • i know that this question asked before but i have never found anything working for my case
  • i have 2 array which is looking like this
Array
(
    [0] => Array
        (
            [`19 January 2021`] => Array
                (
                    [0] => Array
                        (
                            [0] => 36
                            [1] => 817
                            [2] => 67
                        )

                )

        )

)
Array
(
    [0] => Array
        (
            [`20 January 2021`] => Array
                (
                    [0] => Array
                        (
                            [0] => 79
                        )

                )

        )

)
  • then i used json_encode() php method to encode this array which will be looking like this
[{"`19 January 2021`":[["36","817","67"]]}][{"`20 January 2021`":[["79"]]}]
  • but when i’m tring to use in js JSON.parse() it’s give me this error enter image description here
  • can anyone help me

Advertisement

Answer

Individually, these two are totally fine JSON strings.

[{"`19 January 2021`":[["36","817","67"]]}]
[{"`20 January 2021`":[["79"]]}]

You can’t parse them both together at the same time just by concatenating them though. Either parse them individually:

JSON.parse(arr1String);
JSON.parse(arr2String);

Or combine them into a single JSON object.

echo json_encode([
    "arr1" => $arr1,
    "arr2" => $arr2
]);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement