如何仅获取一个作为 JSON 发送的参数值


How to get only one argument value sent as JSON

我的网页链接是这样的

http://mysite.php?json={"some_key":"some_val"}

$json=$_GET ['json'];  
$obj = json_decode($json);
#this line print the whole input 
echo $json."</br>";
#but this does not print anythig 
echo $obj->{'some_key'}."</br>";

我是 php 的新手,我卡在阅读输入中...... :(

**编辑

我的观点是将some_val变成变量,这样我就可以根据值做出决定......

对我来说,这是将其放入 url 中的最简单方法,但如果必须,我将实现 POST 方法。

我需要的代码应该看起来像这样

$variable=(读取键"some_key"的值)

汇总

输入

 http://urlblabla/test.php?json={"some_key":"some_val"}

一些补丁后的代码:

从 URL 表单解码为正常表单,并删除 HTML 实体

法典:

<?php
if(IsSet($_GET['json']))
{
    $json = html_entity_decode( urldecode( $_GET['json'] ) ); 
    $obj = json_decode($json);
    #this line print the whole input 
    echo $json."</br>";
    #but this does not print anythig 
    echo "Value of some_key : '{$obj->some_key}'</br>";
    #into variable (assign some_key value to variable $var)
    $var = $obj->some_key;
    echo "Value of var : '{$var}' <br />";
}
?>

我认为您正在寻找基本的 url 查询参数:

http://mysite.com/index.php?somekey=someval

在 php 中:

if ( isset( $_GET['somekey'] ) && $_GET['somekey'] == 'someval') {
   $variable = $_GET['somekey']; // example of assigning GET param to variable
   echo "do something!";
}

如果您打算将 json 作为 url 参数传入:

$json = json_decode($_GET['json']);
echo $json->some_key;