1

下面是我的工作 api,但结果为"echo $data"is only"array"和 for "echo $data["data"]["operatorid"]"is nothing 屏幕变为空白。那么如何转换json reponsephp字符串并从中获取特定值

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: XXXXXXXX"               
)
);

$context = stream_context_create($opts);
$res = file_get_contents('https://tariff-plan-api-datayuge.p.mashape.com/index.php?circleid=kerala&limit=50&operatorid=BSNL&recharge_type=top', false, $context);
$data = (json_decode($res, true));
echo $data["data"]["operatorid"];

json响应:

{
"data": [
{
  "id": "3148",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "150",
  "recharge_talktime": "150",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 150 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3149",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "250",
  "recharge_talktime": "250",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 250 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3150",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "550",
  "recharge_talktime": "550",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 550 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3151",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1000",
  "recharge_talktime": "1100",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1000 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3152",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1100",
  "recharge_talktime": "1210",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1100 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3153",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1500",
  "recharge_talktime": "1650",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1500 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3154",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "2000",
  "recharge_talktime": "2200",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 2000 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
}
]
4

2 回答 2

1

该 JSON 字符串转换为一个 PHP 对象,该对象具有一个名为的属性,该属性data是一个对象数组

输出自print_r()

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 3148
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 150
                    [recharge_talktime] => 150
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 150 By BSNL
                    [recharge_longdesc] => Full Talktime (offer valid upto 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [1] => stdClass Object
                (
                    [id] => 3149
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 250
                    [recharge_talktime] => 250
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 250 By BSNL
                    [recharge_longdesc] => Full Talktime (offer valid upto 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [2] => stdClass Object
                (
                    [id] => 3150
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 550
                    [recharge_talktime] => 550
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 550 By BSNL
                    [recharge_longdesc] => Full Talktime (offer valid upto 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [3] => stdClass Object
                (
                    [id] => 3151
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 1000
                    [recharge_talktime] => 1100
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 1000 By BSNL
                    [recharge_longdesc] => Extra Talktime (offer is valid till 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [4] => stdClass Object
                (
                    [id] => 3152
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 1100
                    [recharge_talktime] => 1210
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 1100 By BSNL
                    [recharge_longdesc] => Extra Talktime (offer is valid till 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [5] => stdClass Object
                (
                    [id] => 3153
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 1500
                    [recharge_talktime] => 1650
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 1500 By BSNL
                    [recharge_longdesc] => Extra Talktime (offer is valid till 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

            [6] => stdClass Object
                (
                    [id] => 3154
                    [operatorid] => BSNL
                    [circleid] => Kerala
                    [recharge_amount] => 2000
                    [recharge_talktime] => 2200
                    [recharge_validity] => Unlimited
                    [recharge_shortdesc] => Recharge Of Rs 2000 By BSNL
                    [recharge_longdesc] => Extra Talktime (offer is valid till 23 Mar 2015)
                    [recharge_type] => Full Talktime
                )

        )

)

因此,要引用单个项目,您需要使用此语法

$context = stream_context_create($opts);
$res = file_get_contents('https://tariff-plan-api-datayuge.p.mashape.com/index.php?circleid=kerala&limit=50&operatorid=BSNL&recharge_type=top', false, $context);

$data = (json_decode($res));
echo $data->data[0]->operatorid;

如果您使用其他变量名可能更容易$data理解

$jsonObj = (json_decode($res));
echo $jsonObj -> data[0] -> operatorid;

或者,如果您正在处理所有事件

$jsonObj = (json_decode($res));
foreach ($jsonObj -> data as $data) {
    echo $data -> operatorid;
}
于 2015-07-18T16:08:42.600 回答
0

$data拥有一个多维数组 - “数据”,它本身拥有多个数组。

echo $data["data"]["operatorid"]不会返回任何内容,因为您没有指定operatorid要从哪些数组中打印。

使用以下内容指定您要定位的数组

$data["data"][*index*]["operatorid"]

或像这样循环遍历您的 $data 数组

foreach( $data as $Adata ){
    foreach( $Adata as $arr ){
        echo $arr['operatorid'];
    }
}
于 2015-07-18T15:55:54.440 回答