I'm trying to get the json property's from an url. But just the first property arrives.
$json = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=' . $item_name);
$obj = json_decode($json, true);
$result = count($obj);
echo $json;
Output is:
"{"success":true}"
instead of:
"{"success":true,"lowest_price":"0,96\u20ac","volume":"143","median_price":"0,89\u20ac"}"
What am i doing wrong?
Related
Link To JSON File: https://pastebin.com/gXcnNnUK
Code I've Tried So Far:
$json = json_decode($json, true);
foreach($json as $data){
echo $data['videoId'];
}
But it's not returning values. Is there any better approach to do it?
On line 3, did you mean to iterate over $arr instead of $json?
$json = file_get_contents('json.json');
$arr = json_decode($json, true);
foreach($arr['contents']['sectionListRenderer']['contents']['2']['itemSectionRenderer']['contents'] as $pero){
$video_id_list .= $pero['compactVideoRenderer']['videoId'].',';
}
I have a PHP code that converts a JSON into an array of two elements.
{"object":"card","id":"card_1"}
But when I try to print the both, the first returns the value and the second only the boolean.
echo 'id = ' . $response["id"];
echo 'object = ' .$response["object"];
Getting this:
id = true
object = card
What is wrong?
It seems, that you use json_decode to convert your JSON data to an array. Use next basic example to get your expected data:
<?php
// Input
$json = '{"object":"card","id":"card_1"}';
$array = json_decode($json, true);
// Specific items
echo 'id = '.$array["id"].'<br>';
echo 'object = '.$array["object"].'<br>';
// All items
foreach($array as $key => $value) {
echo $key.": ".$value."<br>";
}
?>
Could you please provide the code you use to convert this JSON into an array ?
This works fine:
$jsonObject = '{"object":"card","id":"card_1"}';
$decodedObject = json_decode($jsonObject);
$object = $decodedObject->object;
$id = $decodedObject->id;
echo "Object: {$object}, ID: {$id}";
How can I get the source, title, issn, author, ... from a json file: JSON file
We tried with:
$new_pmid = $_POST['new_pmid'];
$api_json_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=".$new_pmid."&retmode=json";
$json = file_get_contents($api_json_url);
$data = json_decode($json, TRUE);
echo $header[0]->result->$new_pmid->title;
....
But nothing happen...
Can you give me the solution for the json file (generated from pubmed database).
Thank you.
You didn't use the $data variable, which stored the decoded data
You decode JSON into $data as array
$title = $data['result'][$new_pmid]['title'];
$issn = $data['result'][$new_pmid]['issn'];
$authors = $data['result'][$new_pmid]['authors'];
--Update--
To get $authors name ,authtype ,... use foreach loop:
foreach($authors as $author){
$name = $author['name'];
$authtype = $author['authtype'];
$clusterid = $author['clusterid'];
}
In addition to the question yesterday: Question
I got multiple (3) Items in the json file:
results.json
["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]
I want echo all three value from "Produktkategorie" and the value "MaxBreite"
It should look like this:
Artikel1 - 250
Artikel2 - 250
Artikel3 - 250
My code looks like this:
$json = file_get_contents('results.json');
$json = json_decode($json, true);
$anzahl = count($json) -1;
$anzahlstart = 0;
while ($anzahlstart < $anzahl) {
$json = json_decode($json[$anzahlstart], true);
$ProduktkategorieFile = $json['Produkt']['Produktkategorie'];
$MaxBreiteFile = $json['Produkt']['Optionen']['MaxBreite'];
echo $ProduktkategorieFile. "-" .$MaxBreiteFile;
$anzahlstart ++;
}
Unfortunately my Code throws a error after passing first line:
Notice: Undefined offset: 1 in
After that I don't get any result.
Kings of coding, could you help me again please :)
Do you need like this?:-
<?php
$json_string = file_get_contents('results.json');
$json = json_decode($json_string, true);
// I hope the above line gives you exact json what you shown to us
foreach ($json as $jso){
$array = json_decode($jso, true);
echo $array['Produkt']['Produktkategorie'].' - '.$array['Produkt']['Optionen']['MaxBreite'];
echo PHP_EOL;
}
Output:-https://eval.in/728430
Note:- If yes then I hope you are able to get other values easily.Thanks
The problem is with the $json variable name: You're reassigning it on this line: $json = json_decode($json[$anzahlstart], true);
Rename this variable and you're good to go!
I would also replace the while loop with a foreach loop as shown in my example:
<?php
//With foreach
$original = '["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]';
$decoded = json_decode($original);
foreach($decoded as $encodedProduct){
$product = json_decode($encodedProduct,true)['Produkt'];
echo $product['Produktkategorie'] . " - " . $product['Optionen']['MaxBreite'] . "\n";
}
//Original fixed code
$json = json_decode($original, true);
$anzahl = count($json);
$anzahlstart = 0;
while ($anzahlstart < $anzahl) {
$decodedJson = json_decode($json[$anzahlstart], true);
$ProduktkategorieFile = $decodedJson['Produkt']['Produktkategorie'];
$MaxBreiteFile = $decodedJson['Produkt']['Optionen']['MaxBreite'];
echo $ProduktkategorieFile. " - " .$MaxBreiteFile . "\n";
$anzahlstart ++;
}
Your problem is, that you are trying to decode an array of json string, instead of the string itself.
so that would be like this now.
$json = file_get_contents('results.json');
$json = json_decode($json[0], true); // notice [0];on this line.
...
After reading the other question, I have had this problem before, but you essentially need to do two things. in your ajax.
$.ajax({
...
data : JSON.stringify(data)
})
This changes an object into a json string,
Then on your server you do the decode.
something like this
$json = json_decode($jsonstringGoesHERE , true);
For more information in understanding the issue, have a look at this other post.
jQuery ajax, how to send JSON instead of QueryString
The API is:
{"status":1,"data":[{"address":"0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe","balance":6000000000000000000,"nonce":null,"code":"0x","name":null,"storage":null,"firstSeen":null}]}
I am trying to get the value from Balance (6000000000000000000). I also need to divide this value with 1000000000000000000 in meantime...
I tried:
$obj = json_decode($json,true);
$address = $obj['address'];
$json = file_get_html('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
$v = (get_object_vars($obj->data));
$balance = $v['balance'];
$_SESSION['balance'] = $balance;
It doesn't work.
The same code works with the API below:
{"status":"success","data":{"address":"LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK","is_unknown":false,"balance":0,"balance_multisig":0,"totalreceived":0.0340576,"nb_txs":11,"first_tx":{"time_utc":"2015-08-30T09:51:05Z","tx":"10f4b55d0bead8d3d84fe27433db20b63368d65bc043c651d59dbe1342d14098","block_nb":"842330","value":0.00704229,"confirmations":251241},"last_tx":{"time_utc":"2016-02-02T16:09:23Z","tx":"c666d3acf6f57fd86a2ccc9537ee022167da408f26193e4abbd7b8148fc518b3","block_nb":"932771","value":-0.01250452,"confirmations":160800},"is_valid":true},"code":200,"message":""}
What am I doing wrong?
Its because you're missing that the address is located in the first data array in the JSON string and in the second JSON string, no array exists inside the data array.
$obj = json_decode($json,true);
$address = $obj['data'][0]['address'];
$json = file_get_contents('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
echo $obj->data[0]->balance;