php 从 json 编码中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12429029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:32:16  来源:igfitidea点击:

php get values from json encode

phpjson

提问by conmen

I have an url passing parameters use json_encode each values like follow:

我有一个 url 传递参数使用 json_encode 每个值,如下所示:

$json = array
(
    'countryId' => $_GET['CountryId'],
    'productId' => $_GET['ProductId'],
    'status'    => $_GET['ProductId'],
    'opId'      => $_GET['OpId']
);

echo json_encode($json);

It's returned a result as:

它返回的结果如下:

{  
  "countryId":"84",
  "productId":"1",
  "status":"0",
  "opId":"134"
}

Can I use json_decodeto parse each values for further data processing?

我可以json_decode用来解析每个值以进行进一步的数据处理吗?

Thanks.

谢谢。

回答by Mihai Iorga

json_decode()will return an object or array if second value it's true:

json_decode()如果第二个值为真,将返回一个对象或数组:

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['countryId'];
echo $json['productId'];
echo $json['status'];
echo $json['opId'];

回答by Vladimir Hraban

json_decode will return the same array that was originally encoded. For instanse, if you

json_decode 将返回与最初编码相同的数组。例如,如果你

$array = json_decode($json, true);
echo $array['countryId'];

OR

或者

$obj= json_decode($json);

echo $obj->countryId;

These both will echo 84. I think json_encode and json_decode function names are self-explanatory...

这些都会回显 84。我认为 json_encode 和 json_decode 函数名称是不言自明的......