1

i need to extract the exam from this json response using php

cb({"data": [{"map": {"exam": ["e", "x", "a", "m"]}, "words": false, "o": ["exam", "exam", "exam"]}]},150)
1
  • Which exam? I see four of them, possibly five. Commented Dec 23, 2010 at 10:00

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.