无法在php中使用decode_json

时间:2015-11-26 07:35:49

标签: php json

我有一个Json数据但是我无法获得所需的响应。

$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';

我想捕捉一些像

这样的内容

id,msisdncountrycode,msisdncountrycode,statuscode,hlrerrorcodeid

以及其他值也请帮助解决这个问题。

4 个答案:

答案 0 :(得分:1)

看看这个:

$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code

答案 1 :(得分:1)

使用此功能(适用于多种结果)。

$d = json_decode($content);

foreach($d->results as $row){
    echo $row->id; //echoes 21390fb46e92
    echo $row->msisdncountrycode; //echoes DE
    echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
    echo $row->hlrerrorcodeid; //echoes 9
}

回声基于当前输入。由于它在循环中,它将打印所有值。

答案 2 :(得分:0)

你可以试试这个。它对我来说很好。

json_decode($content)

答案 3 :(得分:0)

试试这个:

<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>