【问题标题】:How to get only one argument value sent as JSON如何仅获取一个作为 JSON 发送的参数值
【发布时间】:2013-10-31 14:05:53
【问题描述】:

我的网页链接是这样的

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”的值)

【问题讨论】:

  • 你为什么在url中发送json???
  • var_dump($_GET) 会救你一命
  • @carter - var_dump($_GET); :)
  • @carter - 手册是我的第二本圣经
  • @lukap $json=html_entity_decode(urldecode($_GET['json']));try_it

标签: php json


【解决方案1】:

汇总

输入

 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 />";

}
?>

【讨论】:

  • 我在 000webhost.com 工作,所以问题可能是他们不支持这种 json 传递...
  • 阅读我的问题!,我发布了到目前为止我编写的代码,但是如果您有一些想法应该与发布代码不同,请不要看我的代码......,我只需要代码这将读取变量中的“some_key”
【解决方案2】:

我认为您正在寻找基本的 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;

【讨论】:

  • 这不是我要找的,我的 json 部分有问题,而不是读取简单参数...
猜你喜欢
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多