Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

javascript - 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...