简体   繁体   English

这是json格式吗?

[英]Is this json format?

I am getting this output: 我得到以下输出:

{"params":{"2":true,"3":true}}

I have printed this as (at PHP side): 我将其打印为(在PHP端):

$data = file_get_contents("php://input");
        print($data);

I want to use that 2 and 3 values using loop at PHP side. 我想在PHP端使用循环使用2和3值。 But as I am trying, it is not json format (its doubt). 但是,正如我正在尝试,它不是json格式(其怀疑)。 So how can i use these values using loop? 那么如何使用循环使用这些值?

$j='{"params":{"2":true,"3":true}}';

$decoded=json_decode($j,1);

print "<pre>\n";
print_r($decoded);
print "</pre>\n";

The second parameter of json_decode() is json_decode()的第二个参数是

'assoc' - When TRUE, returned object s will be converted into associative array 'assoc'-当为TRUE时,返回的对象s将转换为关联数组

<?php

$json='{"params":{"2":true,"3":true}}';

$params=json_decode($json, true)['params'];

foreach ($params as $k =>$v){
    echo $k . ' is ' .  var_export($v, true) . PHP_EOL;
}

output: 输出:

2 is true
3 is true
$data='{"params":{"2":true,"3":true}}';
$decoded=json_decode($data,1);
        foreach ($decoded as $key => $param)
        {
            foreach ($param as $user_id => $value)
            {
                    echo $user_id;
            }
        }

It gives me output: 它给我输出:

 2 3

(as I expected). (如我所料)。

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

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