在关键名称中带有一个点的PHP JSON数组

问题描述:

我有下面的JSON,这是来自API的响应。键名在他们的名字中有点,而我使用的编码不会识别它。在关键名称中带有一个点的PHP JSON数组

JSON:

{ 

    "unit.count": 40413, 
    "device.count": 4893, 
    "registration.count": 3951 
} 

我用来阅读的代码如下。

CODE:

$myArray = json_decode($response,true); 

echo $myArray['unit.count']; 

没有回显的

值任何帮助,将不胜感激。

罗伯特

发布以来,我已经找到了API发送JSON脚本的问题。

感谢大家回答。

+1

从我所看到的,一切都按预期工作。你想要达到什么目标? – Sirko

+0

在我身边没有显示任何东西。只是一个空白页 –

+0

你可以尝试与波纹管的答案? –

你的代码工作正常。请确认您有有效的JSON和你正在使用正确的变量

$json = '{ 

    "unit.count": 40413, 
    "device.count": 4893, 
    "registration.count": 3951 
}'; 

$myArray = json_decode($json,true); 

echo $myArray['unit.count']; //op: 40413 

在这里看到一个例子:https://eval.in/817059

试试这个

//convert json format to array is important just pass second parameter as true 
    $arr = array( 
        "unit.count" => 40413, 
        "device.count" => 4893, 
        "registration.count"=>3951 
); 

    $response= json_encode($arr); 
    $myArray = json_decode($response,true); 

    echo $myArray['unit.count']; //40413 
+0

第二个参数已经是真的检查看问题'$ myArray = json_decode($ response,true);' –

请尝试这下面的代码。

<?php 
    $response = '{ 

"unit.count": 40413, 
"device.count": 4893, 
"registration.count": 3951 
}'; 

$myArray = json_decode($response,true); 

echo $myArray['unit.count']; 

?>