i need to extract the exam
from this json response using 开发者_StackOverflow社区php
cb({"data": [{"map": {"exam": ["e", "x", "a", "m"]}, "words": false, "o": ["exam", "exam", "exam"]}]},150)
The problem here is that the answer is wrapped in a callback function cb()
which is not valid JSON. The JSON part is the parameter that is passed to this function (everything between and including {...}
). So the first step is to remove this "outer function":
$json = trim($json, 'cb(),150');
$data = json_decode($json, true);
$exam = $json['data'][0]['map']['exam'];
Reference: trim
, json_decode
, arrays
This only works if the number at the end only consists of 1, 5 or 0. You can either add all digits to the second parameter of trim
or use a combination of strripos
and substr
to chop off everything after }
.
精彩评论