2016-08-14 139 views
0

我是JavaScript和PHP的新手。我已經閱讀了多個堆棧的答案,但我的JSON字符串有點不同。如果你問我,其實很簡單。多維JSON數組到多〜php陣列不工作

的字符串如下:

[[{"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}] 

現在我想將它與json_decode($jsonstring, true)解碼,但它只是當我被它的指數調用它沒有得到一個值。只要我嘗試使用echo $jsonstring[0]獲取數據,我就得到[$jsonstring[0]['width']甚至不返回任何東西。

我說他們錯了嗎?還是別的嗎?

+0

缺少右']? – guest271314

+0

你有一個矩陣/表;一組數組。首先你需要解碼json'$ table = json_decode($ jsonstring,true)'然後你可以得到一個Object'$ row = $ table [0];''item = $ row [0];'現在你可以獲取屬性如'width''echo'width:'。 $ item ['width'];'或'var_dump($ item);'。你需要了解你正在處理widh的數據結構。 – Thomas

回答

0

添加 ']' 字符串之後:`位於字符串的末尾

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

     ) 

) 
0

U可以使用

Var x=JSON.parse(Expression) . 

現在你可以訪問使用x變量JSON對象的字符串解析JSON。

+0

不要忘記標記正確答案 –