I am trying to get a json result to an array object .I am echoing a json data in php. like this.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type:application/json;charset=utf-8');
$arr= '[{
"id": "1",
"date": "2020-03-21",
"status": "present",
"studentid": "1"
},
{
"id": "2",
"date": "2020-03-24",
"status": "present",
"studentid": "1"
},
{
"id": "3",
"date": "2020-03-25",
"status": "absent",
"studentid": "1"
},
{
"id": "4",
"date": "2020-03-26",
"status": "absent",
"studentid": "1"
}
]';
echo $arr;
?>
~
How to get absentees using angular in an array.
Angular part i tried didnt work
this.http.post("http://localhost/android/Api.php?apicall=getattendance", JSON.stringify(this.postData),options)
.subscribe( (data) => {
this.setUsersArray(data);
console.log(data + "URL DATA"+JSON.stringify(this.postData));
}
);
=====================================================================
setUsersArray(data){
if (data instanceof Array) {
{
this.date_present = data.map(function (ele) {
if(ele.status==='present')
{
return ele.date;
}
});
this.date_absent = data.map(function (ele) {
if(ele.status==='absent')
return ele.date;
});
}
}
I am getting date_absent and date_present as null.Why am i getting this as null.Please help me. I am new to angular.
try for loop in typescript
setUsersArray(data:any){
for(let item of data){
console.log(item);
if(item.status==='present') {
this.present.push(item.date);
}
if(item.status==='absent') {
this.absent.push(item.date);
}
}
Trying to this url
$characters = json_decode($data, true); // decode the JSON feed and make an associative array
https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/
Related
Ive got the following JSON:
{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}
I also have another JSON that gives me the ID I want to search for.
For example: "d671ac7d-3b5a-4777-8510-6e8e58295061".
I want to search for the JSON Object, that contains that ID and get the value of the name key. I tried with loops and if, else's but I didn't manage to get it working.
Thanks for your help!
decode the json as array object then loop through with the ID that u want to search
<?php
$json = '{
"servers": [
{
"id": "f34c0185-4c9e-40fd-82f6-1d6e9a5d499e",
"name": "vm01"
},
{
"id": "d671ac7d-3b5a-4777-8510-6e8e58295061",
"name": "vm02"
},
{
"id": "h59j23cc-9ve2-4508-1277-85y1lo27562m",
"name": "vm03"
}
]
}';
$j = json_decode($json, true);
foreach($j['servers'] as $arr)
{
if( $arr['id'] == 'd671ac7d-3b5a-4777-8510-6e8e58295061' ) echo $arr['name'];
}
demo: https://3v4l.org/0DboX
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 a quite nested JSON file. How can I delete an array that includes my value? For ex: I want to delete {"customer":"Customer1","date":"2017-06-03"...} from JSON file and I already knew the "Customer1"
{
"info": [{
"customer": "Customer1",
"date": "2017-06-03",
"beacons": [{
"data1": "1234",
"data2": "Test1",
}, {
"data1": "0088",
"data2": "Test2",
}]
},{
"customer": "Customer2",
"date": "2017-06-03",
"beacons": [{
"data1": "dcdd4",
"data2": "Test3",
}, {
"data1": "0088",
"data2": "Test4",
}]
}]
}
Thanks!
There is some issue in your json data. This is not valid json data; I have decoded the josn data & then check if "customer" value = 'Customer1', then remove the array from main array.
It should be like this:
$jsonData = '{"info ": [{
"customer ": "customer1 ",
"date ": "2017 - 06 - 03 ",
"beacons ": [{
"data1 ": "1234",
"data2 ": "Test1"
}]
}, {
"customer": "customer2 ",
"date": "2017 - 06 - 04 ",
"beacons": [{
"data1": "dcdd4",
"data2": "Test3"
}]
}]
}';
$myData = json_decode($jsonData,true);
foreach($myData["info"] as $k=>$arr) {
if($arr["customer"] == "customer1") {
unset($myData["info"][$k]);
}
}
one small correction in the previous answer .
please call function like this . the searching value should be a value like astring format .
searchObj (myobj, 'Customer1');
make the object to assign to one obj example :=>
var myobj={"info": [{ ...... }]}
.this below function will work for the searching the exact value in object array
call the function with the parameters as like below
searchObj (myobj, Customer1);
function searchObj (obj_name, searchingval) {
for (var key in obj_name) {
var value = obj_name[key];
if (typeof value === 'object') {
searchObj(value, searchingval);
}
if (value === searchingval) {
console.log('property name=' + key + ' property value=' + value);
}
}
}
I would know, why JSON encoded data (from MongoDB via PHP) presented as:
{
"53a6e02221360e263e000000": {
"_id": {
"$id": "53a6e02221360e263e000000"
},
"title": "1"
},
"53a6e02221360e263e000001": {
"_id": {
"$id": "53a6e02221360e263e000001"
},
"title": "2"
}
}
is bad, but:
[
{
"id": "53a6e02221360e263e000000",
"title": "1"
},
{
"id": "53a6e02221360e263e000001",
"title": "2"
}
]
is good?
Is any idea? Both valid JSON.
PHP code example:
// MongoDB init before it
$cursor = $mongo->db->collection->find();
// For not large data
$result = iterator_to_array( $cursor );
$json = json_encode($result);
var_dump($json);
function ajax()
{
$('form').submit(function() {
console.log($(this).serializeArray());
$('#result').text(JSON.stringify($(this).serializeArray()));
return false;
});
}
After this form I'm getting the json data:
[
{
"name": "firstName",
"value": "fsdfdf"
},
{
"name": "lastName",
"value": "df"
},
{
"name": "emailAddress",
"value": "refdfdfd.56#gmail.com"
},
{
"name": "password",
"value": "fdfdddd"
},
{
"name": "phoneNumber",
"value": "fdfdf"
}
]
I'm sending it to the server by this response.php
<?php
header('Access-Control-Allow-Origin: *');
$json = "http://ec2-54-201-121-123.us-west-2.compute.amazonaws.com:8080/refer247/registration";
$jsonfile = file_get_contents($json);
var_dump($jsonfile);
echo json_encode($_POST);
var_dump(json_decode($jsonfile));
echo json_decode($jsonfile);
?>
But after this I am getting json parser error. What actually is happening? I don't know. When I include datatype as text then my success function is calling but I want to send to the server only json data. What I'm doing wrong here...?
I appreciate if i get some help. Thanks.