Cant access JSON data with PHP - php

im trying to access the ID and SENT from this JSON but is not working for me.
UPDATED
$json = '{
"response": {
"sent": true,
"message": "Sent to 57304",
"id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
}
}';
json_decode($json);
$id=$json->id;
$sent=$json->sent;
echo "</br> id:".$id."</br>";
echo "</br> sent:".$sent."</br>";

Use the php json_decode() function. Like this:
$json = '{
"response": {
"sent": true,
"message": "Sent to 57304",
"id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
}
}';
$json = json_decode($json, true);
$json = (object) $json['response'];
echo $json->sent; // output: 1
echo $json->id; // output: "gBEGVzBChXFYAgmcOrfFpGem8qw"
1 is the equivalent for a boolean value of true

It Works,There are two label of JSON object,so you cant access inner object directly. you should assign json_decode($json) into a php object like $obj=json_decode($json); here is the working file
<?php
$json = '{
"response": {
"sent": true,
"message": "Sent to 57304",
"id": "gBEGVzBChXFYAgmcOrfFpGem8qw"
}
}';
// convert assign json data into php object
$obj=json_decode($json);
//All the inner key can be access throug 'response' object
$id=$obj->response->id;
$sent=$obj->response->sent;
echo "</br> id:".$id."</br>";
echo "</br> sent:".$sent."</br>";

Related

Access to a nested array on a JSON in PHP?

This is the code, it use a foreach to access to department and workers .
EDIT: Will correct several paste errors
<?php
$json = '{
"boss": "Jeff",
"department": [{
"office": "1111",
"workers": "[{\"id_work\":\"123\",\"name\":\"mike\",\"mobile\":\"12345\"}]"
}]}';
$json_data = json_decode($json);
echo "Boss:".$json_data->boss;
echo "<br>";
foreach($json_data->deparment as $dep)
{
echo "Office number:".$dep->office."<br>";
foreach($dep->workers as $worker){
echo "Worker ID: ".$worker->id_work."<br>";
echo "Worker name : ".$worker->name."<br>";
echo "Worker mobil: ".$worker->mobil."<br>";
}
}
?>
I cannot access to internal array when I try do a foreach() this happens :
Invalid argument supplied for foreach()
How I can access to the information of the nested array
The json data inside $json is wrong. The decode didn't gave errors but the array you get back is not how you want it to be, that's why you get errors after json_decode.
The error you got was because the worker value is in string format.
You should update that:
From: "workers": "[{\"id_work\":\"123\",\"name\":\"mike\",\"mobile\":\"12345\"}]"
To: "workers": [{ "id_work": "123", "name": "mike", "mobile":"12345"}]
End result:
$json = '{
"boss": "Jeff",
"department": [{
"office": "1111",
"workers": [{ "id_work": "123", "name": "mike", "mobile":"12345"}]
}]}';
$json_data = json_decode($json);
echo "Boss:".$json_data->boss;
echo "<br>";
foreach($json_data->department as $dep)
{
echo "Office number:".$dep->office."<br>";
foreach($dep->workers as $worker){
echo "Worker ID: ".$worker->id_work."<br>";
echo "Worker name : ".$worker->name."<br>";
echo "Worker mobil: ".$worker->mobile."<br>";
}
}

Parsing through json string php

im new with JSON and have had massive struggle trying to parse this JSON
{
"730": {
"success": true,
"data": {
"price_overview": {
"currency": "USD",
"initial": 1499,
"final": 1499,
"discount_percent": 0
}
}
}
}
I have tried different approaches but still struggle to get the value of initial in price_overview
You need to json_decode, and then, just use the dictionary that is generated to grab the value. Like this:
$json = <<< JSON
{
"730": {
"success": true,
"data": {
"price_overview": {
"currency": "USD",
"initial": 1499,
"final": 1499,
"discount_percent": 0
}
}
}
}
JSON;
$json_a = json_decode($json, true);
echo $json_a['730']['data']['price_overview']['initial'];
CodePad
http://codepad.org/i1ELBxd9
Further Reading
Using JSON with PHP
Try this:
<?php
$data = '{"730":{"success":true,"data":{"price_overview":{"currency":"USD","initial":1499,"final":1499,"discount_percent":0}}}}';
$json = json_decode(trim($data), true);
echo '<pre>';
print_r($json[730][data][price_overview]);
echo '</pre>';

PHP Json Parsing 6

How do I pull the 76561198216468627 from
{ "response": { "steamid": "76561198216468627", "success": 1 } }
I've tried this but it pulls 1 instead of the id
foreach ($parsed_json->{'reponse'} as $item) {
$steamid = $item;
}
You need to decode the JSON string into either a PHP object or array. Here's how to do it each way:
ARRAY METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString, true);
foreach($json as $item)
{
$steamid = $item['steamid'];
}
OBJECT METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString);
foreach($json as $item)
{
$steamid = $item->steamid;
}
Try this:
$json = '{
"response": [{ "steamid": "76561198216468627", "success": 1 }]
}';
$decode = json_decode($json);
echo $decode->response[0]->steamid;
Then you can get 76561198216468627

Extract each level data with php

The following is the JSon data.
{
"statusType": "OK",
"entity": [
{
"category": "category1","difficultyLevel": "Easy",
"quizAnswerChoices": [{"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}, {"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}]
}
],
"entityType": "java.util.ArrayList",
"status": 200,
"metadata": {}
}
I need to parse
- entity
- quizAnswerChoices (count the item)
How to retrieve each choiceText etc
Use
$json='{
"statusType": "OK",
"entity": [
{
"category": "category1","difficultyLevel": "Easy",
"quizAnswerChoices": [{"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}, {"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}]
}
],
"entityType": "java.util.ArrayList",
"status": 200,
"metadata": {}
}';
$encodedJson= json_decode($json,true);
$quizAnswerChoices=$encodedJson['entity'][0]['quizAnswerChoices'];
echo 'Count: '.count($quizAnswerChoices);
Use this
$arry = json_decode($json,true);
foreach ($arry['entity'] as $ke => $ve) {
foreach ($ve['quizAnswerChoices'] as $k => $v) {
print_r($v);
}
}
Lets consider you are recieving this json in a variable called "$contents".
All you have to do is decode it:
$decoded = json_decode($contents, true); //True so, the decoded object will be converted into an ssociative array
print_r($decoded);
If you do a json_decode like
$data = json_decode(json_data);
Then $data will be a traversable array
$data['entity']['quizAnswerChoices'] etc
Use json_decode to convert json to array
$dataArray = json_decode($json_string, true)
$dataArray['entity']; // entity
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
count($dataArray['entity']['quizAnswerChoices']); // quizAnswerChoices count

cant retrieve JSON object on localhost

I'm trying to retrieve JSON Objects on my localhost, the issue is that it wont output anything. The JSON objects could look like following:
[
{
NAME: "Hearthstone",
PLAYER1: "Rdu ",
PLAYER2: "Savjz ",
status: 2,
meta: "LIVE"
},
{
NAME: "League of Legends",
PLAYER1: "Team King ",
PLAYER2: "EDG ",
status: 2,
meta: "28.12."
}]
php retrieve objects.
$url = "http://localhost:8888/crawl_JSON.php";
$json = file_get_contents($url);
$json_output = json_decode($json);
echo $json_output;
Why don't it output anything?
Note that json_decode() returns an object, you cannot echo you will need to use var_dump or print_r. If you want to echo , you can echo the JSON string.
$url = "http://localhost:8888/crawl_JSON.php";
$json = file_get_contents($url);
echo $json;
$json_output = json_decode($json);
var_dump($json_output);
And inside of crawl_JSON.php You need to echo the JSON, you will need to make sure it is valid.
<?php
echo '
[
{
"NAME": "Hearthstone",
"PLAYER1": "Rdu ",
"PLAYER2": "Savjz ",
"status": 2,
"meta": "LIVE"
},
{
"NAME": "LeagueofLegends",
"PLAYER1": "TeamKing",
"PLAYER2": "EDG",
"status": 2,
"meta": "28.12."
}
]
';

Categories