개발! 딱 깔끔하고 센스있게!

PHP JSON 디코드-stdClass 본문

개발 스크랩 메모/PHP

PHP JSON 디코드-stdClass

렉사이 2020. 12. 13. 23:57

2djson 문자열 생성 문제에 대한 질문이 있습니다.

지금 나는 왜 다음 내용을 방문할 수 없는지 알고 싶다:

$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 

대답

비슷한 그룹의 문법으로 접근하고 있습니다:

echo j_string_decoded['urls'][1]; 

상대로 되돌아가다.

두 번째 인자를 true 로 지정한 다음 그룹으로 바꾸기:

$j_string_decoded = json_decode($json_str, true); 

제작:

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

아니면 이것을 시도해 보세요:

$j_string_decoded->urls[1] 

대상에 쓰이는 -> 동작 부호.

문서 인용:

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

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

Comments