php - PHP check JSON string for item = value
问题描述
I need to get the value one one particular value in my json string
the json string comes from an api url http://url:port/api.php?action=reg_user&sub=list
and outputs like so
[{"id":"1","username":"username1","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1511883014","date_registered":"1421604973","email":"test1@my-email.com","ip":"8.8.8.8","status":"1"},{"id":"31","username":"username2","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1539813642","date_registered":"1473632400","email":"test2@my-email.com","ip":"8.8.8.8","status":"1"},
i would like to check the value of credits where username = username1
then if credits > 30 do x, else do y
I presume first of all I need to decode this jason string
so i tried to do
$api_result = file_get_contents( 'http://url:port/api.php?action=reg_user&sub=list' );
$json_decode = json_decode($api_result);
echo $json_decode;
expecting this to echo the array in a nicely formatted structure, instead it just outputs the word Array
. Where do I go from here?
Thanks
UPDATE 1...
SO i checked again and I defo can echo $api_result so I know that the file_get_contents
is working fine i tried the two comments suggestions however both didn't work...
one of the suggestions was to make my php $api_result = file_get_contents( 'http://url:port/api.php?action=reg_user&sub=list' ); $json_decode = json_decode($api_result); echo $json_decode['username'];
here is a screenshot of how the echo $api_result is formatted in case this isnt a propper json string
so to me this looks like it opens with (and is all contained in a pair of [] and then each result is enclosed in {} as I'd expect with a json string right? however i thought json strings used {} and arrays used {} so to me this looks like a string inside an array???
I looked at php.net's resource on JSON DECODE and tried var_dump(json_decode($json));
which did print me this
UPDATE 2
I just tried https://www.functions-online.com/json_decode.html and pasted the data in, it was able to decode the data absolutely fine,
It then gave me a copy if the json_decode sample at the bottom but this didn't work either, returning a null value like json_decode doesn't work on my server?
解决方案
From your second screenshot (please cut & paste the text instead!) you can see the decoded JSON is an array of objects with properties id
, username
etc. So you can access them using object notation. This code uses the snippet of your api_result from your original question to demonstrate how to do what you want:
$api_result = '[{"id":"1","username":"username1","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1511883014","date_registered":"1421604973","email":"test1@my-email.com","ip":"8.8.8.8","status":"1"},{"id":"31","username":"username2","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1539813642","date_registered":"1473632400","email":"test2@my-email.com","ip":"8.8.8.8","status":"1"}]';
$json = json_decode($api_result);
foreach ($json as $user) {
if ($user->username == 'username1') {
if ($user->credits > 30) {
// do x
echo "user $user->username has more than 30 credits ($user->credits)";
}
else {
// do y
echo "user $user->username has less than or equal to 30 credits ($user->credits)";
}
}
}
Output:
user username1 has less than or equal to 30 credits (0)
推荐阅读
- python - 将字符串连接/附加到文本文件中的数字列表
- node.js - 如何从 mongoDB 数据库中的 express 模块中导出
- android - 无法将序列化库依赖项编译到我的 AAR 中
- python - 具有上限的numpy数组的重新分配
- regex - perl 正则表达式匹配挑战在 Windows 上查找文件夹和子文件夹的大小
- java - 有什么办法可以让程序在同一行重复,同时删除之前写的内容?
- python - 使用 PyGitHub 库获取成员的最后提交日期
- php - PHP 严格标准:在 .lib 中只能通过引用传递变量
- javascript - 带有时区的 Javascript 倒计时计时器
- python-3.x - 创建具有 {key:list[]} 的 dict 元素的子集