使用空格访问json结果

时间:2014-05-01 04:57:48

标签: php json

我从API获得了json结果。在问题中提出的关键之一,而不是像这样的真正的关键:

 {"somthing":"12345","questions":[{"Question Here about something. There are also quotes like this \"here\")":"thevalue"}],"id":"123455"}

"问题这里有关于某事。还有这样的引号\"这里\"" 将始终保持不变,但如何访问其值(值)。

我尝试过类似的事情:

$result = json_decode($jsonresult);

echo $result->questions->Question Here about something. There are also quotes like this \"here\");

但由于空格和转义引号,这不起作用。有什么建议?

2 个答案:

答案 0 :(得分:3)

尝试下面的内容:

$jsonresult  =  '{"somthing":"12345","questions":[{"Question Here about something. There are also quotes like this \"here\")":"thevalue"}],"id":"123455"}';
$result      =  json_decode($jsonresult);
echo $result->questions[0]->{'Question Here about something. There are also quotes like this "here")';

这将导致thevalue

答案 1 :(得分:0)

更简单的方法是将JSON字符串转换为PHP关联数组,如下所示:

$json_string = '{"somthing":"12345","questions":[{"Question Here about something. There are also quotes like this \"here\")":"thevalue"}],"id":"123455"}';   

$json = json_decode($json_string, true);

$questions = $json['questions'];

for($i = 0; $i < count($questions); $i++) {
    $question = $questions[$i];
    foreach($question as $key => $value) {
        echo "Question: " . $key . "\n";
        echo "Answer:" . $value . "\n";
    }
}