-1

我尝试从 json 文件中获取两条信息:“Principal”和 WiFiMAC。我需要它来导出名称和 MAC 以导出到数据库中。

{
"messages":
          {"message":"150 devices(s) returned."},
           "devices":
                   {"device":
            [
                    {"@id":"121","uuid":"xxx-xxx-xxxxxxxxx-xxx-x-xxxxx","principal":"John Rambo","blockReason":0,"clientId":"anything","compliance":0,"countryCode":49,"countryId":68,"countryName":"Germany","details":
                            [
                            {"entry":[
                                    {"key":"Model","value":"anything"},
                                    {"key":"AvailableDeviceCapacity","value":57.360877990722656},
{"key":"WiFiMAC","value":"123456789012"}]}]}]}}

和更多的键。

我可以像这样提取主体:

$json = file_get_contents("test.xml");
$json = json_decode($json);
echo $json->messages->message;
echo $json->devices->device[1]->principal;

但我没有机会提取 WiFiMAC。对于json中的任何设备,我都需要这个,我想我可以用foreach加载每个设备......但第一个是提取WiFiMAC。

4

1 回答 1

0

已复制您的要求,请访问以下链接: http ://codepad.org/MhgkTloe

$ret = json_decode($json, true);
// the principal
echo $ret['devices']['device'][0]['principal'];
// This is the path to WiFiMac
echo $ret['devices']['device'][0]['details'][0]['entry'][2]['key'];
于 2012-06-14T11:04:38.467 回答