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
  }
 ]
}

私は以下を試しましたが、Googleマップの地理的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

説明はここにありますが、スキップして最終的なコードをさらに下にきれいにすることができます。

$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上記の 1 と 2 の macaddresses と同様に、ループを設定できます。それらをループで追加すること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 に答える
0

json から配列へ:

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

配列からJsonへ

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