I'm trying to parse Json result below fromAjax response. Foreach CostingItem i need to access to UnitPrice.Amount, but I got error.
Can someone help me to access items?
I got this response from Laravel based webservice.
{
"CostingItem": [{
"#attributes": {
"Description": "Basic Price",
"Quantity": "1",
"PassengerRPH": "1",
"PriceBasis": "Passenger"
},
"UnitPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"ExtendedPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"Commission": {
"#attributes": {
"Rate": "11.00",
"Type": "P"
}
}
}, {
"#attributes": {
"Description": "Basic Price",
"Quantity": "1",
"PassengerRPH": "2",
"PriceBasis": "Passenger"
},
"UnitPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"ExtendedPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"Commission": {
"#attributes": {
"Rate": "11.00",
"Type": "P"
}
}
}]
}
here is the script used in Ajax:
$.ajax({
type: "POST",
url: "/public/fetch-bambini",
data: {
id: d,
tipologia: tipologia,
bambini: bambini,
neonati: neonati,
adulti: adulti
},
success: function(response) {
$.each(response.CostingItem, function(k, v) {
console.log(v);
});
}
});
This return this response:
Related
I am new to PHP and have little experience with PHP arrays, I have this below JSON file.
$json2=
'{
"location": "westus",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_D1_v2"
},
"storageProfile": {
"imageReference": {
"sku": "2016-Datacenter",
"publisher": "MicrosoftWindowsServer",
"version": "latest",
"offer": "WindowsServer"
},
"osDisk": {
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "Standard_LRS"
},
"name": "myVMosdisk",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/{existing-nic-name}",
"properties": {
"primary": true
}
}
]
},
"osProfile": {
"adminUsername": "{your-username}",
"computerName": "myVM",
"adminPassword": "{your-password}"
},
"diagnosticsProfile": {
"bootDiagnostics": {
"storageUri": "http://{existing-storage-account-name}.blob.core.windows.net",
"enabled": true
}
}
}
}';
I want to update the values for 'location', 'vmSize','sku','publisher','offer' in the above JSON,
"location" : "eastus"
"vmSize" : "Standard_D3_v2"
"sku" : "20h1-evd"
"publisher" : "MicrosoftWindowsDesktop"
"offer" : "windows-10"
I have tried to do the following to this but I am nowhere near navigating the array correctly.
$arr = json_decode($json2, true);
print_r($arr);
Can anyone please help here?
Just try this code
<?php
$json2=
'{
"location": "westus",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_D1_v2"
},
"storageProfile": {
"imageReference": {
"sku": "2016-Datacenter",
"publisher": "MicrosoftWindowsServer",
"version": "latest",
"offer": "WindowsServer"
},
"osDisk": {
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "Standard_LRS"
},
"name": "myVMosdisk",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/{existing-nic-name}",
"properties": {
"primary": true
}
}
]
},
"osProfile": {
"adminUsername": "{your-username}",
"computerName": "myVM",
"adminPassword": "{your-password}"
},
"diagnosticsProfile": {
"bootDiagnostics": {
"storageUri": "http://{existing-storage-account-name}.blob.core.windows.net",
"enabled": true
}
}
}
}';
$arr = json_decode($json2, true);
$arr['location'] = "eastus";
$arr['properties']['hardwareProfile']['vmSize'] = "Standard_D3_v2";
$arr['properties']['storageProfile']['imageReference']['sku'] = "20h1-evd";
$arr['properties']['storageProfile']['imageReference']['publisher'] = "MicrosoftWindowsDesktop";
$arr['properties']['storageProfile']['imageReference']['offer'] = "WindowsServer";
$updatedJson = json_encode($arr);
print_r($updatedJson);
First you need to try json_decode to make an array from this json file and then based on the array schema you need to find the property that you want and update it and then again use the json_encode to make an json
I have list of countries and their states in JSON file, below is how it looks like
"NO": {
"name": "Norway",
"states": {
"VA": {
"name": "Vest-Agder"
},
"RO": {
"name": "Rogaland"
},
"HO": {
"name": "Hordaland"
},
"SF": {
"name": "Sogn og Fjordane"
},
"MR": {
"name": "Møre og Romsdal"
},
"ST": {
"name": "Sør-Trøndelag"
},
"NO": {
"name": "Nord-Trøndelag"
},
"NT": {
"name": "Nordland"
},
"TR": {
"name": "Troms"
},
"FI": {
"name": "Finnmark"
},
"SJ": {
"name": "Svalbard"
},
"SJ": {
"name": "Jan Mayen"
},
"AK": {
"name": "Akershus"
},
"AA": {
"name": "Aust-Agder"
},
"BU": {
"name": "Buskerud"
},
"HE": {
"name": "Hedmark"
},
"OP": {
"name": "Oppland"
},
"OS": {
"name": "Oslo"
},
"TE": {
"name": "Telemark"
},
"VF": {
"name": "Vestfold"
},
"OF": {
"name": "Østfold"
}
}
What I am trying to do is to list the state names based on country selection in dropdown menu. Let say if Norway is selected as country, the dropdown will contain all the state names in list and short name in value
I created the code below, but getting empty dropdown
$conobjitems = json_decode($jsonitemfile);
echo "<select>";
$findBystatename = function($constatename) use ($conobjitems) {
foreach($conobjitems->Norway as $short){
echo "<option>". $short . "</option>";
}
};
echo "</select>";
Need advise here.
This is the correct code that I came up with
echo "<select>";
foreach($conobjitems->PK->states as $short => $outputstate) {
echo "<option value=". $short . ">". $outputstate->name . "</option>";
}
echo "</select>";
I have a JSON file that have all list of all the countries with their cities, and state. The file has this structure
"NO": {
"name": "Norway",
"states": {
"VA": {
"name": "Vest-Agder"
},
"RO": {
"name": "Rogaland"
},
"HO": {
"name": "Hordaland"
},
"SF": {
"name": "Sogn og Fjordane"
},
"MR": {
"name": "Møre og Romsdal"
},
"ST": {
"name": "Sør-Trøndelag"
},
"NO": {
"name": "Nord-Trøndelag"
},
"NT": {
"name": "Nordland"
},
"TR": {
"name": "Troms"
},
"FI": {
"name": "Finnmark"
},
"SJ": {
"name": "Svalbard"
},
"SJ": {
"name": "Jan Mayen"
},
"AK": {
"name": "Akershus"
},
"AA": {
"name": "Aust-Agder"
},
"BU": {
"name": "Buskerud"
},
"HE": {
"name": "Hedmark"
},
"OP": {
"name": "Oppland"
},
"OS": {
"name": "Oslo"
},
"TE": {
"name": "Telemark"
},
"VF": {
"name": "Vestfold"
},
"OF": {
"name": "Østfold"
}
}
},
What I am trying to achieve is to get the short name of state when user input the full name in the input field. For example if they add "Oslo" I will get "OS" in output
This is the code that I came up with but I am not getting the output.
$jsonitem = file_get_contents("countries-info.json");
$objitems = json_decode($jsonitem);
$findByname = function($name) use ($objitems) {
foreach ($objitems as $friend) {
if ($friend->name == $name) return $friend->state;
}
return false;
};
echo $findByname($_GET[code]) ?: 'No record found.';
Need advise.
Looking at the structure of your JSON you should loop $objitems->NO->states ( $objitems will contain the complete object, you only want the states)
So you need to change your foreach to:
foreach ($objitems->NO->states as $short => $state) {
if ($state->name == $name) return $short;
}
foreach ($objitems as $state => $friend) {
if ($friend->name == $name) return $state;
}
I am making a login app in ionic using PHP, I have a login function which works well but I need to be able to store certain values to local storage for later use.
Here is a sample from my code:
$scope.login = function() {
Login.login($scope.login).then(function (data) {
if (Object.keys(data.data).length === 1) {
var datao = angular.toJson(data);
$scope.profile = datao;
$scope.email = $scope.profile.email;
$scope.username = $scope.profile.username;
console.log(data);
console.log($scope.profile);
// $state.go('dashboard');
}
....
});
....
});
$scope.profile displays the following:
[Log]
{
"data": [{
"id": "5",
"username": "ozombo",
"reputation": "ceo",
"activate": "8425",
"followercount": "3",
"praycount": "0",
"donetour": "0",
"fb_id": "0",
"fullname": "Adebambo Oyelaja",
"church": "RCCG ",
"photo": "1417451697.jpg",
"twtr_id": "0",
"screen_name": "",
"email": "oyexs911#yahoo.com",
"bio": "Geek, Father, Husband",
"country": "Nigeria",
"state": "Lagos",
"followscount": "0",
"account": "",
"password": "5cf1bc1b9a2ada1ae9e29079aae1aefa",
"signup_date": "1413305984",
"signupdevice": "web",
"keycode": "5fc868f0767b0c164341a9e8e35edbe4",
"last_login": "1436519269",
"city": "Palmgroove",
"campaign": "",
"bday": "29-09-1988",
"gender": "Male",
"approved": "0",
"donewelcome": "0",
"phonenumber": "07017993683",
"secure": "private",
"churchid": "1",
"views": "68",
"marital": "Married"
}],
"status": 200,
"config": {
"method": "GET",
"transformRequest": [null],
"transformResponse": [null],
"url": "http://localhost:8888/opb/api/login.php?user=oyexs911#yahoo.com&pass=oyelaja",
"headers": {
"Accept": "application/json, text/plain, /"
}
},
"statusText": "OK"
}
How will I get the user email for example
You can get the email by accesing to data.email like var email = $scope.profile.data.email
You forgot that you have data before you can get the email.
{
"data": [{
"username": "ozombo",
"email": "oyexs911#yahoo.com",
...
}]
}
You can get access to the user email by iterating over the data array:
$scope.profile.data.forEach(function(d) {
console.log(d.email);
});
Hope that helps!
I have been getting my facebook friends work history. But the result is an array whose content is as follows :
{
"id": "xxx",
"friends": {
"data": [
{
"name": "Indiarto Priadi",
"work": [
{
"employer": {
"id": "111178415566505",
"name": "Liputan 6 SCTV"
}
},
{
"employer": {
"id": "107900732566334",
"name": "SCTV"
}
}
],
"id": "502163984"
},
{
"name": "Agustin Kertawijaya",
"work": [
{
"employer": {
"id": "138215336225242",
"name": "Trader Corporation (Canada)"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "168673399850791",
"name": "Desktop operator <Pre press>"
},
"start_date": "2006-06-01",
"end_date": "2008-06-01"
},
{
"employer": {
"id": "114464911939560",
"name": "Merrill Corporation Canada"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "190075304347365",
"name": "Financial Print Project Manager"
},
"start_date": "2006-06-01",
"end_date": "2011-10-01"
}
],
"id": "511990261"
}
],
"paging": {
"next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}
}
}
Now i want to display "only" the employer name on my page, but i can't find the way to do that.
Here is my code :
$userFriends = $facebook->api('/'.$userId.'/friends');
for($i=0;$i<=3;$i++){ //retrieved friends (max 4 persons)
$value=$userFriends["data"][$i];
echo "<div id='$value[id]'>";
$profile = $facebook->api("/".$value["id"]);
$friendsWork = $facebook->api("/".$value["id"]."?fields=work");
echo "<strong>".$profile["name"]."<br></strong>";
echo " <img src='https://graph.facebook.com/".$value["id"]."/picture'><br>";
echo "Username : ".$profile["username"]."<br>";
echo "Local : ".$profile["locale"]."<br>";
echo "Birthdate : ".$profile["birthday"]."<br>";
print_r($friendsWork); // <--- the result is an array
echo "<hr>";
echo "</div>";
}
Does anyone know how to display the company(employer) name only?
any answers would be greatly appreciated. Thank You
Here you go, nothing fancy, I just did what I said in my comment
$json = '{
"id": "xxx",
"friends": {
"data": [
{
"name": "Indiarto Priadi",
"work": [
{
"employer": {
"id": "111178415566505",
"name": "Liputan 6 SCTV"
}
},
{
"employer": {
"id": "107900732566334",
"name": "SCTV"
}
}
],
"id": "502163984"
},
{
"name": "Agustin Kertawijaya",
"work": [
{
"employer": {
"id": "138215336225242",
"name": "Trader Corporation (Canada)"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "168673399850791",
"name": "Desktop operator <Pre press>"
},
"start_date": "2006-06-01",
"end_date": "2008-06-01"
},
{
"employer": {
"id": "114464911939560",
"name": "Merrill Corporation Canada"
},
"location": {
"id": "110941395597405",
"name": "Toronto, Ontario"
},
"position": {
"id": "190075304347365",
"name": "Financial Print Project Manager"
},
"start_date": "2006-06-01",
"end_date": "2011-10-01"
}
],
"id": "511990261"
}
],
"paging": {
"next": "https://graph.facebook.com/1065251285/friends?limit=2&fields=name,work&offset=2&__after_id=511990261"
}}}'; // remember the last two closing curlys, you left them out of the code block in the OP.
$obj = json_decode($json);
foreach ($obj->friends->data as $friend) {
echo '<h1>' . $friend->name . '</h1>';
foreach ($friend->work as $job) {
echo 'Employee: ' . $job->employer->name . '<br/>';
}
echo '<hr>';
}
Output:
Indiarto Priadi
Employee: Liputan 6 SCTV
Employee: SCTV
--------------------------------------
Agustin Kertawijaya
Employee: Trader Corporation (Canada)
Employee: Merrill Corporation Canada
--------------------------------------
You just have to parse the array you obtained, like this-
$userFriends = $facebook->api('/'.$userId.'/friends?fields=work,name');
foreach($userFriends['data'] as $friend)
{
$friend_name = $friend['name'];
$emp_name=array();
// list of all employers of this friend
if(isset($friend['work']))
{
foreach($friend['work'] as $work)
{
array_push($emp_name, $work['employer']['name']);
}
}
else
{
$emp_name = "N.A.";
}
}