1
This topic differs from subjects such as: "How to consume JSON in PHP"; "Extract json value in php"; "Read string json on php" or "Recover JSON in PHP" Although there is co-relation on various subjects of the PHP language, and the JSON format, it addresses a specific problem, detailed and summarized.
In all the materials that deal with the subject of JSON in PHP, I can’t find one that cites the different ways to treat JSON. They treat json as if it is always expressed in the same way, which is not true in practice. So we always have an algorithm for each way it is expressed. There is a universal way to capture json data, or there is a specific way for the second case cited below?
A practical example would be that:
{
"friendslist":{
"friends":[
{
"steamid":"76561197960265731",
"relationship":"friend",
"friend_since":0
},
{
"steamid":"76561197960265738",
"relationship":"friend",
"friend_since":0
},
{
"steamid":"76561197960265740",
"relationship":"friend",
"friend_since":0
},
{
"steamid":"76561197960265747",
"relationship":"friend",
"friend_since":0
}
]
}
}
Surely it’s different from that:
[
{
"id":"578",
"valor":"4.00",
"CLIENTE":{
"id":"492",
"nome":"MARIA",
"sobrenome":"Machado",
"endereco":"Avenida das Am\u00e9ricas",
"latitude":null,
"longitude":null
},
"dataCompra":"DATA_AQUI",
"PRODUTOS":[
{
"id":"14135",
"codigoDeBarras":"7896015516260",
"nome":"SONRIDOR",
"detalhes":"500mg cx 60 comp",
"categoria":"medicamento",
"quantidade":"2",
"precoUnitario":".10"
}
],
"FRANQUIA":{
"id":"818",
"nomeFantasia":null,
"razaoSocial":null,
"rede":{
"id":"32",
"nome":"Sapataria João"
},
"endereco":"Rua Acre",
"latitude":"-22.899079",
"longitude":"-43.181612"
}
}
]
The first case, until I find a way of reading
$steamid_player = "76561198112612121";
$apikey = "APIKEY";
$amg = file_get_contents("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=$apikey&steamid=$steamid_player&relationship=friend");
$decode = json_decode($amg, TRUE);
foreach ($decode["friendslist"]["friends"][0] as $valor){
$steamid = $valor["relationship"]->steamid;
echo $steamid;
}
But in the second case, I did not find practical examples. I can do at most a vardump.
"They treat JSON as if it were always expressed in the same way". It is always expressed in the same way. It is a standard to represent data and if you do not follow this pattern it will be an invalid JSON. The difference between the presented JSON is the data they represent. What do you mean by "universal way"? (Your PHP code does not work)
– Woss
It’s... There are no "ways" to write the same format in another JSON structure. It might be interesting to take a look at the structure of objects in JS to better understand it. :)
– Paulo Martins
[".... The difference between the presented JSON ..."] If there are differences in the presented JSON, JSON is not expressed in the same way, by definition. In the same way that there are no married singles. And yes, there are differences in the way that is "expressed" in both cases. The first case for example, passes an expressed data collection and a sub-collection afterwards. Both arrays, such that the first array represents an entire collection. In the second case, you pass 3 interdependent arrays, without expressing an antecedent collection. PHP is working normally in my environment.
– Paulo Sérgio Duff
To whom interested problem, answer script did not work on PHP5 but on 7.
– Paulo Sérgio Duff