1

I want to create a array for the following json code.

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

I have tried with the following but it is showing parsing error in google maps geomatic api

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

$jsonVal =  json_encode($a);

can anyone help me?

1
  • the error shows that you don't have to send the data in this format. Just check in google which kind of encoded data google maps api needed. Commented Dec 12, 2012 at 12:13

3 Answers 3

1

PHP's json_encode does not wrap integers with double quotes, which is invalid json. Try this:

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

$jsonVal =  json_encode($a);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response. I have tried with doublu quote also.Still it is not parsing
{ "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } }-- this is the error i'm getting
if you are using curl to make the request, you might consider CURLOPT_HTTPHEADER => array('Content-Type: application/json') param for curl_setopt_array call.
0

From json to Array:

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

From Array to Json

$json = json_encode(/* array Object */);

Comments

0

explanations here but you can skip to clean final code further down.

$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" );

Then combine all cell towers

  $allCellTowers[] = $cellTower1;

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

Now for MAC addresses and 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...

just as for cellTower1, cellTower2 the macaddresses 1 & 2 above can be populated with a loop. Adding them to wifiAccessPoints also can be done in a loop but it done manually below just so you understand.

  $wifiAccessPoints[] = $macAddress1;

  $wifiAccessPoints[] = $macAddress2;

finally the other elements all go in the resulting array to encode

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

IN CLEAN CODE IT IS

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.