简体   繁体   English

如何使用php从这个json响应中获取值

[英]how to get the value from this json response using php

我需要使用php从此json响应中提取exam

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. 这里的问题是答案被包装在无效的JSON回调函数cb()中。 The JSON part is the parameter that is passed to this function (everything between and including {...} ). JSON部分是传递给此函数的参数(介于{...}之间(包括{...}之间的所有东西))。 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 参考: trimjson_decodearrays

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 } . 仅当末尾的数字仅由1、5或0组成时,此方法才有效。您可以将所有数字添加到trim的第二个参数中,也可以使用strripossubstr的组合来strripos }之后的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM