1

我想为以下 json 代码创建一个数组。

{
 "homeMobileCountryCode": 310,
 "homeMobileNetworkCode": 260,
 "radioType": "gsm",
 "carrier": "T-Mobile",
 "cellTowers": [
  {
   "cellId": 39627456,
   "locationAreaCode": 40495,
   "mobileCountryCode": 310,
   "mobileNetworkCode": 260,
   "age": 0,
   "signalStrength": -95
  }
 ],
 "wifiAccessPoints": [
  {
   "macAddress": "01:23:45:67:89:AB",
   "signalStrength": 8,
   "age": 0,
   "signalToNoiseRatio": -65,
   "channel": 8
  },
  {
   "macAddress": "01:23:45:67:89:AC",
   "signalStrength": 4,
   "age": 0
  }
 ]
}

我尝试了以下方法,但它在谷歌地图地理 api 中显示解析错误

$a = array("homeMobileCountryCode" => 310,
    "homeMobileNetworkCode" => 260,
    "radioType" => "gsm",
    "carrier" => "T-Mobile");

$jsonVal =  json_encode($a);

谁能帮我?

4

3 回答 3

1

PHP 的 json_encode 没有用双引号包裹整数,这是无效的 json。试试这个:

$a = array("homeMobileCountryCode" => "310",
    "homeMobileNetworkCode" => "260",
    "radioType" => "gsm",
    "carrier" => "T-Mobile");

$jsonVal =  json_encode($a);
于 2012-12-12T12:02:37.387 回答
0

从 json 到数组:

$array = json_decode(/* json text /*);

从数组到 Json

$json = json_encode(/* array Object */);
于 2012-12-12T12:00:10.723 回答
0

这里有解释,但您可以跳过以进一步清理最终代码。

$cellTower1 = array(  "cellId"=> "39627456",
   "locationAreaCode"=> "40495",
   "mobileCountryCode"=> "310",
   "mobileNetworkCode"=> "260",
   "age"=> "0",
   "signalStrength"=> "-95" );

$cellTower2 = array(  "cellId"=> "2222222",
   "locationAreaCode"=> "22222",
   "mobileCountryCode"=> "222",
   "mobileNetworkCode"=> "222",
   "age"=> "22",
   "signalStrength"=> "-22" );

然后合并所有手机信号塔

  $allCellTowers[] = $cellTower1;

  $allCellTowers[] = $cellTower2; 
//etc... or could be in a loop

现在是 MAC 地址和 wifiAccessPoints。

 $macAddress1 = array (
   "macAddress"=> "01:23:45:67:89:AB",
   "signalStrength" => "8",
   "age" => "0",
   "signalToNoiseRatio" => "-65",
   "channel" => "8"
  );

 $macAddress2 = array (
   "macAddress" => "01:23:45:67:89:AC",
   "signalStrength" => "4",
   "age" => "0"
  );

 $macAddress3 = etc...

就像cellTower1, cellTower2上面的 macaddresses 1 & 2 可以用循环填充一样。添加它们wifiAccessPoints也可以在循环中完成,但它在下面手动完成,以便您理解。

  $wifiAccessPoints[] = $macAddress1;

  $wifiAccessPoints[] = $macAddress2;

最后,其他元素都进入结果数组进行编码

$myarray = array( "homeMobileCountryCode"=> "310",
                  "homeMobileNetworkCode"=> "260",
                  "radioType"=> "gsm",
                  "carrier"=> "T-Mobile",
                  "cellTowers"=>$allCellTowers, 
                  "wifiAccessPoints" => $wifiAccessPoints
            );
$json = json_encode($myarray);

在干净的代码中

$cellTower1 = array(  "cellId"=> "39627456",
   "locationAreaCode"=> "40495",
   "mobileCountryCode"=> "310",
   "mobileNetworkCode"=> "260",
   "age"=> "0",
   "signalStrength"=> "-95" );

$allCellTowers[] = $cellTower1;

 $macAddress1 = array (
   "macAddress"=> "01:23:45:67:89:AB",
   "signalStrength" => "8",
   "age" => "0",
   "signalToNoiseRatio" => "-65",
   "channel" => "8"
  );

 $macAddress2 = array (
   "macAddress" => "01:23:45:67:89:AC",
   "signalStrength" => "4",
   "age" => "0"
  );

 $wifiAccessPoints[] = $macAddress1;

 $wifiAccessPoints[] = $macAddress2;

 $myarray = array( "homeMobileCountryCode"=> "310",
                   "homeMobileNetworkCode"=> "260",
                   "radioType"=> "gsm",
                   "carrier"=> "T-Mobile",
                   "cellTowers"=>$allCellTowers, 
                   "wifiAccessPoints" => $wifiAccessPoints
             );

 //note that you have your first key missing though in your example

 $json = json_encode($myarray);
于 2012-12-12T12:25:11.803 回答