cant retrieve JSON object on localhost - php

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."
}
]
';

Related

Cant access JSON data with 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>";

Not getting the array on JSON

Hello I am trying to break down JSON data
** {
"message": "gradeExam.php",
"id": "171",
"student_id": "dfd",
"questions": [{
"question_id": "0",
"student_input": "def doubly_"
},
{
"question_id": "1",
"student_input": "asd"
}
]
}**
using PHP
$json = file_get_contents('php://input');
$_POST = json_decode($json, true);
$input = $_POST["questions"];
foreach($input as $questions)
{
$quest_id[] = $questions["question_id"];
$student_input[] = $questions["student_input"];
}
echo $quest_id;
echo $student_input;
BUT keep getting a response of.. ArrayArrayArray[]
I am trying loop and get the data of question_id: 0, student_input: def doubly_, question_id: 1, student_input: asd.
What am I doing wrong?
Everything that you have done is correct except these 2 lines.
echo $quest_id;
echo $student_input;
As $quest_id and $student_input are arrays we cannot use the echo function. Instead, you can use the print_r function as below
print_r($quest_id);
print_r($student_input);

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>";
}
}

How to parse json array using php foreach?

I want to get id,url,img and title of videos in the JSON data.My current code doesn't output anything
could any one tell me what i am doing wrong.Thanks
$code2 = stripslashes($_POST['outputtext']);
$data = json_decode($code2, true);
$i = 0;
foreach($data->videos as $values)
{
echo $values->id . "\n";
echo $values->url . "\n";
echo $values->img . "\n";
echo $values->title . "\n";
$i++;
}
data:
{
"cat": {
"id": "1234567",
"source_id": null,
"title_en": "first season",
"description_en": "This is spring category ",
},
"videos": [{
"id": "312412343",
"url": "\/2015-07-17\/1abcd.mp4",
"img": "image\/44\/\/2015-07-17\/1abcd.jpg",
"title": "first",
}, {
"id": "2342343",
"url": "\/2015-07-16\/2dcdeg.mp4",
"img": "images\/44\/\/2015-07-16\/2dcdeg.jpg",
"title": "second",
}];
}
validated json data:
{
"cat":{
"id":"1234567",
"source_id":null,
"title_en":"first season",
"description_en":"This is spring category "
},
"videos":[
{
"id":"312412343",
"url":"\/2015-07-17\/1abcd.mp4",
"img":"image\/44\/\/2015-07-17\/1abcd.jpg",
"title":"first"
},
{
"id":"2342343",
"url":"\/2015-07-16\/2dcdeg.mp4",
"img":"images\/44\/\/2015-07-16\/2dcdeg.jpg",
"title":"second"
}
]
}
Because you call
$data = json_decode($code2, true);
your $data is an array and not an object as you try to access it. So either access it as regular array, or change 2nd argument of json_decode() to false (or removeit as this is default), as this is what controls conversion behavior.
See docs: http://php.net/manual/en/function.json-decode.php
Your data is not a valid JSON (; at the end, redundant and missing comas - it's simply broken).
In case of such problems, var_dump() is pretty helpful to inspect what data you are really working with.

How to Parse JSON with PHP?

I can do a var_dump, but when trying to access the values, I am getting errors about the values not being found.
{
"metrics": {
"timers": [
{
"name": "com.android.timer.launchtime",
"startTime": 1232138988989,
"duration_ms": 1900
},
{
"name": "com.android.timer.preroll-load-time",
"startTime": 1232138988989,
"duration_ms": 1000
}
]
}
}
I used the following so far to try and parse it.
$json_file = file_get_contents('test.json');
$json_a = json_decode($json_file,true);
var_dump(json_decode($json_file)); //This works
echo $json_a['name']; //I want to print the name of each (from timers).
Try:
$yourDecodedJSON = json_decode($yourJson)
echo $yourDecodedJSON->metrics->timers[0]->name;
Or you can:
$yourDecodedJSON = json_decode($yourJson, true); // forces array
echo $yourDecodedJSON['metrics']['timers'][0]->name;
In your case, you may want to..
foreach($yourDecodedJSON['metrics']['timers'] as $timer){
echo $timer['name']; echo $timer['duration_ms']; // etc
}
If something fails, use:
echo json_last_error_msg()
To troubleshoot further
You need do it in following manner:-
<?php
$data = '{
"metrics": {
"timers": [
{
"name": "com.android.timer.launchtime",
"startTime": 1232138988989,
"duration_ms": 1900
},
{
"name": "com.android.timer.preroll-load-time",
"startTime": 1232138988989,
"duration_ms": 1000
}
]
}
}';
$new_array = json_decode($data); //convert json data into array
echo "<pre/>";print_r($new_array); //print array
foreach ($new_array->metrics->timers as $new_arr){ // iterate through array
echo $new_arr->name.'<br/>'; // rest you can do also same
}
?>
Output:- https://eval.in/407418

Categories