1

Lets say I have this JSON string:

{
 "kind": "youtube#searchListResponse",
 "etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/aEjB0uWRf7BN9_0Kkzd0ZK9uqkw\"",
 "nextPageToken": "CAEQAA",
 "pageInfo": {
  "totalResults": 76061,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/T9Y8RT9FLOEgb7ql2XZUv7PpAGU\"",
   "id": {
    "kind": "youtube#channel",
    "channelId": "UCcB3bcWy0_QK7uPQvTD0LwQ"
   }
  }
 ]
}

How would I extract the channelId from that as a new variable?

1
  • you don't extract from json. you decode the json to a native data structure (e.g. php object/array), and then access the data in that array/object like you would any OTHER object/array. Commented Jun 23, 2015 at 20:52

2 Answers 2

4

You would use json_decode:

$json = /* your json string */;
$obj = json_decode($json);
$channel = $json->items[0]->id->channelId;
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you need to decode json data using json_decode()and then you can get values. So do like below:-

$json = $json_string;// suppose your json string variable name is $json_string
$std_obj_data = json_decode($json); // now you will get STD class Object
$channel = $std_obj_data->items[0]->id->channelId; // fetch channelId;

For example:- https://eval.in/386415

1 Comment

Said I had to wait 6 minutes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.