Php json decode array - php

i have a json string. But i can not access values.
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$content = file_get_contents($json_string);
$json_a = json_decode($content, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
How can i fix this code? Thank you

$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
// you don't need this line
//$content = file_get_contents($json_string);
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];

There's no need to use file_get_contents if you're storing the JSON in a string and then decoding it. Follow the below approach:
$json_string = '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name']; // rapertuar
echo $json_a['05526']['0']['notes']['0']['mat1']; // 59

Related

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

Can't print a simple JSON?

I know it's basic, but I couldn't get what's wrong because it's not my area.
I am sending some request to a server and print the response like this:
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_GET,1);
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($rest);
$json = json_decode($response, true);
echo $response;
Where I get this:
{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}
Then, I am trying to print the JSON or its fields, but I then get nothing:
echo $json;
echo $json['UserID'];
echo $json['Devices'];
To clarify the comments a bit:
$str = '{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}';
$json = json_decode($str, true); // to an associative array
// to echo the UserID
echo "userID : " . $json['results'][0]['UserID'];
// output: userID : 433011AC-228A-4931-8700-4D050FA18FC1
// to get the structure of the json array in general use print_r() as AbraCadaver pointed out
print_r($json);
In your attempt you were missing the results[0] part.

Decode G2A search results (json) to array

Help me to grab name of games from url
This page output json format.
I try to convert it to array, but my code not work. Please help me!
$url = 'https://search.g2a.com/items/select?json.wrf=jQuery111003403934023808688_1411464896728&q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757';
$content = file_get_contents($url);
$json = json_decode($content, true);
echo "<pre>";
print_r($json);
echo "</pre>";
You should remove the JSONP parameter json.wrf from the URL first:
https://search.g2a.com/items/select?q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757
This will return a proper JSON result.
The output from that URL (that I get currently starts with):
jQuery111003403934023808688_1411464896728({"responseHeader" ...
This isn't a pure JSON response, but rather a JSONP response.
If you're just trying to parse it in PHP, maybe something like:
$url = ...; // Your URL Here
$data = file_get_contents($url);
$pos = strpos($data, '{');
$data = substr($data, $pos, strlen($data) - $pos - 2);
$json = json_decode($data, true);
echo "<pre>";
print_r($json);
echo "</pre>";

get data from JSON file (with cURL)

I would like to trnsform some JSON data to a php array, this is my code:
<?php
$obj1=json_decode('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json', true);
$championname = $obj1[data][aatrox][name];
echo $championname;
?>
The problem is that i don't know how to get the data from http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json
How can i make this code work?
Try with file_get_contents() like this:
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json , true);
$championname = $obj1['data']['aatrox']['name'];
echo $championname;
Use file_get_contents to download a file.
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json, true);

php picasa api json decode problem

Here is my code to json decode picasa api results, but it always Warning: Invalid argument supplied for foreach(), Where is the problem? Thanks.
<?php
header('Content-type:text/html; charset=utf-8');
$url="http://picasaweb.google.com/data/feed/base/all?alt=json&kind=photo&access=public&filter=1&q=usa&imgor=landscape&max-results=50&hl=en";
$json = file_get_contents($url);
$data = json_decode($body, true);
foreach ($data['feed']['entry'] as $result){
echo html_entity_decode($result->content->src, ENT_QUOTES, 'UTF-8');
echo html_entity_decode($result['updated']['$t'], ENT_QUOTES, 'UTF-8');
}
?>
json tree here
You are using a wrong variable:
$json = file_get_contents($url);
$data = json_decode($body, true);
It should be $json instead of $body.
You used json_decode(, true) so it converts all objects to associative arrays. But here you use object-notation to access your data which might result in another error:
echo html_entity_decode($result->content->src, ENT_QUOTES, 'UTF-8');

Categories