2010-11-02 26 views
9

Ben şu erişemez neden bilmek istiyorum Şimdi bir 2D JSON stringPHP JSON kod çözme - stdClass

yapılmasıyla ilgili bir soruya başlamıştı:

$json_str = '{"urls":["http://example.com/001.jpg","http://example.com/003.jpg","http://example.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}'; 

$j_string_decoded = json_decode($json_str); 
// echo print_r($j_string_decoded); // OK 

// test get url from second item 
echo j_string_decoded['urls'][1]; 
// Fatal error: Cannot use object of type stdClass as array 

cevap

22

Bu dizi benzeri sözdizimi ile erişen: nesnesi döndürülür

echo j_string_decoded['urls'][1]; 

ise. bunu yapma

$j_string_decoded = json_decode($json_str, true); 

:

true ikinci argüman belirterek diziye dönüştürün

$json_str = '{"urls":["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}'; 

$j_string_decoded = json_decode($json_str, true); 
echo j_string_decoded['urls'][1]; 

Veya deneyin:

-> operatör nesneler için kullanılan
$j_string_decoded->urls[1] 

Bildirimi . Dokümanlarınızdan aktaran

:

uygun PHP türü json kodlanmış değeri döndürür. Değerler true, false ve null (büyük/küçük harf duyarlı) sırasıyla TRUE, FALSE ve NULL olarak döndürülür. json kodu çözülemezse veya kodlanmış veriler özyineleme sınırından daha derinse NULL döndürülür. Eğer $j_string_decoded->urls[1]

olarak değere erişmek istiyorum Yoksa, birleştirici diziler dönmek zorunda json_decode($json_str,true) gibi ek bir argüman geçebileceği böylece varsayılan olarak

http://php.net/manual/en/function.json-decode.php

+0

harika cevap, şerefsiz Sarfraz – FFish

+0

@ FISH: Hoşgeldiniz :) – Sarfraz

5

Kullanım:

json_decode($jsonstring, true); 
Bir dizi döndürmek için

.

+0

ile uyumlu olur mu? : vvvvvv –

7

json_decode PHP nesneleri JSON sözlükleri döner hangi daha sonra $j_string_decoded['urls'][1]

+0

Açıklama için thanx! – FFish