I want to get each item from geoplugin array separately.
Here is my example code -
$location_info = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']);
echo$location_info;
and here the response in array
a:24:{s:17:"geoplugin_request";s:12:"76.180.83.10";s:16:"geoplugin_status";i:200;s:15:"geoplugin_delay";s:3:"1ms";s:16:"geoplugin_credit";s:145:"Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.";s:14:"geoplugin_city";s:7:"Buffalo";s:16:"geoplugin_region";s:8:"New York";s:20:"geoplugin_regionCode";s:2:"NY";s:20:"geoplugin_regionName";s:8:"New York";s:18:"geoplugin_areaCode";s:0:"";s:17:"geoplugin_dmaCode";s:3:"514";s:21:"geoplugin_countryCode";s:2:"US";s:21:"geoplugin_countryName";s:13:"United States";s:14:"geoplugin_inEU";i:0;s:19:"geoplugin_euVATrate";b:0;s:23:"geoplugin_continentCode";s:2:"NA";s:23:"geoplugin_continentName";s:13:"North America";s:18:"geoplugin_latitude";s:7:"42.9297";s:19:"geoplugin_longitude";s:8:"-78.7434";s:32:"geoplugin_locationAccuracyRadius";s:1:"5";s:18:"geoplugin_timezone";s:16:"America/New_York";s:22:"geoplugin_currencyCode";s:3:"USD";s:24:"geoplugin_currencySymbol";s:5:"$";s:29:"geoplugin_currencySymbol_UTF8";s:1:"$";s:27:"geoplugin_currencyConverter";s:1:"1";}
Example - I want to get city and echo it from this array
$city = ['geoplugin_city'];
echo$city;
You forgot to do $location_info = unserialize($location_info); right after the connection.
Using a PHP file, I echoed data from a MySQL database in the following format: [{"longitude":"-122.031","latitude":"37.3323”}]
In my Swift file, I use NSJSONSerialization.JSONObjectWithData() to create a JSON dictionary that looks like the following
(
{
latitude = "37.3323";
longitude = "-122.031";
}
)
If I wanted to store the individual dictionary values inside variables, I believe the following snippet of Swift code would work
class Location: NSObject {
var latitude: String?
var longitude: String?
}
var locations = [Location]()
if let locationDictionary = jsonDictionary["coordinates"] as? [String: AnyObject] {
let location = Location()
location.setValuesForKeysWithDictionary(locationDictionary)
print(location.latitude, location.longitude)
}
The only problem I’m having is that I need my JSON dictionary to look like
(
coordinates = {
latitude = "37.3323";
longitude = "-122.031";
}
)
but I don’t know what steps to take to achieve this.
You would have to make your JSON like this:
[{"coordinates":{"longitude":"-122.031","latitude":"37.3323"}}]
which is an array of dictionaries, where each dictionary contains another one.
And here is your code adapted for the new format:
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []), // your source may differ, I'm just using this line for my example
content = json as? [[String:AnyObject]] { // the array of dictionaries
for item in content {
if let locationDictionary = item["coordinates"] as? [String: AnyObject] { // the dictionary inside the dictionary
let location = Location()
location.setValuesForKeysWithDictionary(locationDictionary)
print(location.latitude, location.longitude)
}
}
}
How can i create one Json with many small sets of data separated by comma?
Instead of one big Json enclosed by double curly brackets?
I do receive a Json and with php i do use foreach to loop over it, making a lot of data processing inside.
Then generate a new Json, just to avoid the data processing on the client side wich will be processed by angularjs ng-repeat.
All the json data is mixed into one big json set (inside double curly brackets)
My goal is to separate into small sets of data.
I can use the NrType property. In this script the NrType receives the last atribution and just the last received is available.
//The php script
$arr = json_decode($returnedJson); //The original json to be pre-processed
$processedData = "[]";
$processedJson = json_decode($processedData,true);
foreach($arr as $key=>$value) {
foreach($value as $vkey=>$vvalue) {
if( $value[$i]->NrType == 1 ) {
$VlMIni = $value[$i]->QttyInitial;
$VlMSub = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
$VlMRec = $value[$i]->RealValue;
$VlMTotal += $VlMesRece;
//much more data processing going on here ...
} elseif( $value[$i]->NrType == 2 ) {
.
.
.
//and much more data processing going on here ...
}
}
//simple data atribution here
$processedJson['labelDesIni'] = 'Instruments';
$processedJson['labelValueMIni'] = $lblVlMIni;
$processedJson['labelValuePIni'] = $lblVlPlIni;
$processedJson['labelValueAIni'] = $lblVlAIni;
$processedJson['labelValuePAIni'] = $lblVlPAIni;
$processedJson['labelValuePercInic'] = $lblVlPercInic;
$processedJson['labelValuePerc2Inic'] = $lblVlPerc2Inic;
//much more data atribution ...
echo json_encode($processedJson); //the new hgenerated Json
The generated Json :
{
labelDesI: "Inspection",
labelValueMI: "2357",
labelValuePlI: "3914066",
labelValueAI: "1389406",
labelValuePAI: "2431425",
labelValuePercI: 57.143691456656,
labelValuePerc2I: 35.497766261478,
labelDesR: "Instruments",
labelValueMR: "734.54",
labelValuePR: "819.14",
labelValueAR: "660.05",
labelValuePAR: "877.94",
labelValuePercR: 80.087,
labelValuePerc2R: 44.739,
labelDesAcfi: "Fiscalização",
labelValueMAcfi: "343",
labelValuePlAcfi: "29907",
labelValueAAcfi: "16718",
labelValuePAAcfi: "16493",
labelValuePercAcfi: 101.36421512157,
labelValuePerc2Acfi: 55.899956531916,
labelDesT: "Totals",
labelValueMT: 365.59,
labelValuePlT: 547.62,
labelValueAnT: 909.63,
labelValuePAnT: 957.63,
labelValuePercT: 22949,
labelValuePerc2T: 25065
}
The desired format Would be this :
{
label: "Inspection",
labelValue1: "2357",
labelValue2: "3914066",
labelValue3: "1389406",
labelValue4: "2431425",
labelValue5: 57.1456656,
labelValue6: 35.4961478
},
{
labelDesR: "Instruments",
labelValue1: "734.54",
labelValue2: "819.14",
labelValue3: "660.05",
labelValue4: "877.94",
labelValue5: 80.087,
labelValue6: 44.739
},
{
labelDesT: "Totals",
labelValue1: 365.59,
labelValue2: 547.62,
labelValue3: 909.63,
labelValue4: 957.63,
labelValue5: 22949,
labelValue6: 25065
}
Thank´s in advance
generate all of your object separately, create an array of these objects and json_encode the array:
$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];
and add it to you main object:
$processedJson[] = $processedJsonElement;
do this for each section of Json you want to represent. Not sure how you're structuring your Foreach loop as your code doesn't match the output, but whatever you structure is whatever you will will output when you call json_encode.
Basically, you need to structure your foreach loop to be able to compartmentalize the objects you wish to represent as an array of json objects.
I'm using Mapquest in an attempt to get latitude and longitude values of a requested address.
This is my call to their API:
$json = file_get_contents('http://www.mapquestapi.com/geocoding/v1/address?key=******&callback=renderOptions&outFormat=json&inFormat=json&json={location:{street:"Lancaster,PA"},options:{thumbMaps:false}}');
$output = json_decode($json, true);
$lat = $output['results'][0]['locations'][0]['latLng']['lat'];
$lng = $output['results'][0]['locations'][0]['latLng']['lng'];
echo $lat.",".$lng;
As you can see, I'm trying to echo the LAT and LNG to screen but it's empty. When I print_r($json), I get the following:
renderOptions({"results":[{"locations":[{"latLng":{"lng":-76.30127,"lat":40.03804},"adminArea4":"Lancaster
County","adminArea5Type":"City"...............
So I know the response is OK and that LAT and LNG are available, but if I print_r($output) I get nothing and if I var_dump($output) I get NULL, and obviously I don't get my LAT LNG data.
I'm not great with JSON and arrays. Can somebody see what I'm doing wrong?
You need to remove &callback=renderOptions from the URL. Right now, the server returns JSONP (with the specified callback function). This is no valid JSON and so json_decode returns NULL (as specified in the manual)
I want to parse this URL with PHP to get the latitude of a specific address (2+bis+avenue+Foch75116+Paris+FR):
I use the google maps web service: http://maps.googleapis.com/maps/api/geocode/json?address=2+bis+avenue+Foch75116+Paris+FR&sensor=false
// Get the JSON
$url='http://maps.googleapis.com/maps/api/geocode/json?address=2+bis+avenue+Foch75116+Paris+FR&sensor=false';
$source = file_get_contents($url);
$obj = json_decode($source);
var_dump($obj);
It doesn't work. I have this error :
OVER_QUERY_LIMIT
So I searched on the web and I found that there is a limitation using google maps api (Min 1 sec between 2 queries and max 2500 queries per day)
Do you know any way to convert an adress to --> Latitude/Longitude ???
You are accessing results incorrectly.
// Get the JSON
$url='http://maps.googleapis.com/maps/api/geocode/json?address=2+bis+avenue+Foch75116+Paris+FR&sensor=false';
$source = file_get_contents($url);
$obj = json_decode($source);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;