多维JSON数组到multi~php数组无法正常工作

时间:2021-11-20 21:29:10

I'm new to JavaScript and PHP. I have read multiple stacks for answers but my JSON string is a little different. It's actually pretty easy if you ask me.

我是JavaScript和PHP的新手。我已经阅读了多个堆栈以获得答案,但我的JSON字符串有点不同。如果你问我,这实际上很容易。

The string is as follows:

字符串如下:

[[{"height":"444","width":"444","picture":"/image/data/122.jpg","x":0,"y":0,"currentheight":"444"},{"height":"444","width":"444","picture":"/image/data/122.jpg","y":"444","x":0,"currentheight":888},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":888,"x":0,"currentheight":1111}],[{"height":"223","width":"444","picture":"/image/data/122.jpg","y":0,"x":444,"currentheight":"223"},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":"223","x":444,"currentheight":446}]

Now I'm trying to decode it with json_decode($jsonstring, true), but it just doesn't get a value when I call it by it's index. As soon as I try to get data by using echo $jsonstring[0] I get [ as the result. $jsonstring[0]['width'] doesn't even return anything.

现在我正在尝试使用json_decode($ jsonstring,true)对其进行解码,但是当我通过它的索引调用它时它只是没有得到值。一旦我尝试使用echo $ jsonstring [0]获取数据,我得到[结果。 $ jsonstring [0] ['width']甚至没有返回任何内容。

Am I calling them wrong or is it something else?

我称他们错了还是别的什么?

2 个解决方案

#1


0  

After adding ']' to the string:

将']'添加到字符串后:

$ cat a.php
<?php
$a='[[{"height":"444","width":"444","picture":"/image/data/122.jpg","x":0,"y":0,"currentheight":"444"},{"height":"444","width":"444","picture":"/image/data/122.jpg","y":"444","x":0,"currentheight":888},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":888,"x":0,"currentheight":1111}],[{"height":"223","width":"444","picture":"/image/data/122.jpg","y":0,"x":444,"currentheight":"223"},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":"223","x":444,"currentheight":446}]]';
print_r(json_decode($a, true));
?>
$ php a.php
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [x] => 0
                    [y] => 0
                    [currentheight] => 444
                )

            [1] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 444
                    [x] => 0
                    [currentheight] => 888
                )

            [2] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 888
                    [x] => 0
                    [currentheight] => 1111
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 0
                    [x] => 444
                    [currentheight] => 223
                )

            [1] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 223
                    [x] => 444
                    [currentheight] => 446
                )

        )

)

#2


0  

U can parse json from a string using

你可以用一个字符串解析json

Var x=JSON.parse(Expression) .

Now You are able to access JSON objects using x variable.

现在,您可以使用x变量访问JSON对象。

#1


0  

After adding ']' to the string:

将']'添加到字符串后:

$ cat a.php
<?php
$a='[[{"height":"444","width":"444","picture":"/image/data/122.jpg","x":0,"y":0,"currentheight":"444"},{"height":"444","width":"444","picture":"/image/data/122.jpg","y":"444","x":0,"currentheight":888},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":888,"x":0,"currentheight":1111}],[{"height":"223","width":"444","picture":"/image/data/122.jpg","y":0,"x":444,"currentheight":"223"},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":"223","x":444,"currentheight":446}]]';
print_r(json_decode($a, true));
?>
$ php a.php
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [x] => 0
                    [y] => 0
                    [currentheight] => 444
                )

            [1] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 444
                    [x] => 0
                    [currentheight] => 888
                )

            [2] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 888
                    [x] => 0
                    [currentheight] => 1111
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 0
                    [x] => 444
                    [currentheight] => 223
                )

            [1] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 223
                    [x] => 444
                    [currentheight] => 446
                )

        )

)

#2


0  

U can parse json from a string using

你可以用一个字符串解析json

Var x=JSON.parse(Expression) .

Now You are able to access JSON objects using x variable.

现在,您可以使用x变量访问JSON对象。