简体   繁体   English

PHP检查项目=值的JSON字符串

[英]PHP check JSON string for item = value

I need to get the value one one particular value in my json string我需要在我的 json 字符串中获取一个特定值的值

the json string comes from an api url http://url:port/api.php?action=reg_user&sub=list and outputs like so json 字符串来自 api url http://url:port/api.php?action=reg_user&sub=list并输出像这样

[{"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我想检查用户名 = username1 的信用值

then if credits > 30 do x, else do y然后如果学分 > 30 做 x,否则做 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 .期望这以格式良好的结构回显数组,而不是它只输出单词Array Where do I go from here?我从这里去哪里?

Thanks谢谢

UPDATE 1...更新 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...所以我再次检查,我 defo 可以回显 $api_result 所以我知道file_get_contents工作正常我尝试了两个评论建议但是都没有工作......

one of the suggestions was to make my php $api_result = file_get_contents( ' http://url:port/api.php?action=reg_user&sub=list ' );建议之一是让我的 php $api_result = file_get_contents( ' http://url:port/api.php?action=reg_user&sub=list ' ); $json_decode = json_decode($api_result); $json_decode = json_decode($api_result); echo $json_decode['username'];回声 $json_decode['用户名'];

here is a screenshot of how the echo $api_result is formatted in case this isnt a propper json string这是如何格式化 echo $api_result 的屏幕截图,以防这不是正确的 json 字符串

编辑 1 - $api_result 的回显

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???所以对我来说,这看起来像它打开 (并且都包含在一对 [] 中,然后每个结果都包含在 {} 中,正如我所期望的那样使用 json 字符串吗?但是我认为 json 字符串使用了 {} 和数组使用 {} 所以对我来说这看起来像一个数组中的字符串???

I looked at php.net's resource on JSON DECODE and tried var_dump(json_decode($json));在 JSON DECODE 上查看了php.net 的资源并尝试了var_dump(json_decode($json)); which did print me this这确实给我打印了这个

在此处输入图片说明

UPDATE 2更新 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,我刚刚尝试了https://www.functions-online.com/json_decode.html并粘贴了数据,它能够很好地解码数据,

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?然后如果底部的 json_decode 样本给了我一个副本,但这也不起作用,返回一个空值,如 json_decode 在我的服务器上不起作用?

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.从您的第二个屏幕截图(请剪切并粘贴文本!)您可以看到解码后的 JSON 是一个对象数组,具有idusername等属性。因此您可以使用对象表示法访问它们。 This code uses the snippet of your api_result from your original question to demonstrate how to do what you want:此代码使用原始问题中的 api_result 片段来演示如何执行您想要的操作:

$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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM