开发者

how to get the value from this json response using php

开发者 https://www.devze.com 2023-01-31 18:31 出处:网络
i need to extractthe exam from this json response using 开发者_StackOverflow社区php cb({\"data\": [{\"map\": {\"exam\": [\"e\", \"x\", \"a\", \"m\"]}, \"words\": false, \"o\": [\"exam\", \"exam\", \"

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 }.

0

精彩评论

暂无评论...
验证码 换一张
取 消