如何在php中读取json作为字符串?

时间:2014-06-14 12:46:33

标签: php json

尊重所有人,

我有一个来自api的字符串。现在我想从这个字符串中提取有用的数据。此输出是Json查询的结果。 字符串是

  

{" PR" {" URL":" http://www.ididthisfilm.com/lex_tmp2/custom-form/""域":" http://www.ididthisfilm.com","标题":"可持续性词典 - 自定义表格","描述":"欢迎来到列表编辑表格","图片":[{"网址":" http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png"," Alt":& #34;可持续性词典","标题":""," SourceType":1," Width":0, "身高":0," ParsedAspectRatio":0}],"画":[]," AUTHORNAME":空,&#34 ; ExtraInfo":空}" EM":空}`

如何在不同的变量中获取标题和描述以及url,以便我可以在php中使用它们。

1 个答案:

答案 0 :(得分:1)

使用内置函数json_decode,您将获得JSON对象。

$str=<<<CODE
{"PR":{"Url":"http://www.ididthisfilm.com/lex_tmp2/custom-form/","Domain":"http://www.ididthisfilm.com","Title":"Lexicon Of Sustainability - Custom Form","Description":"Welcome to THE LIST EDIT FORM","Pictures":[{"Url":"http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png","Alt":"Lexicon Of Sustainability","Title":"","SourceType":1,"Width":0,"Height":0,"ParsedAspectRatio":0}],"Videos":[],"AuthorName":null,"ExtraInfo":null},"EM":null}  
CODE;

print_r( json_decode($str) );

输出

stdClass Object
(
    [PR] => stdClass Object
        (
            [Url] => http://www.ididthisfilm.com/lex_tmp2/custom-form/
            [Domain] => http://www.ididthisfilm.com
            [Title] => Lexicon Of Sustainability – Custom Form
            [Description] => Welcome to THE LIST EDIT FORM
            [Pictures] => Array
                (
                    [0] => stdClass Object
                        (
                            [Url] => http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png
                            [Alt] => Lexicon Of Sustainability
                            [Title] => 
                            [SourceType] => 1
                            [Width] => 0
                            [Height] => 0
                            [ParsedAspectRatio] => 0
                        )

                )

            [Videos] => Array
                (
                )

            [AuthorName] => 
            [ExtraInfo] => 
        )

    [EM] => 
)

如果发生任何错误,请尝试以下操作:json_last_error()