How to access a JSON element which has ':' in its name using PHP
Tag : php , By : TheDave1022
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I am parsing a RSS feed using PHP but the problem is that is has some names like: , one possible way $name_space = 'im:collection';
$con->$name_space->lable ...
/* or */
$con->{'im:collection'}->label ...
|
access a deep Json element in a multidimentional JSON array
Date : March 29 2020, 07:55 AM
like below fixes the issue In general, avoid using eval. The JSON object has a parse method for converting from strings to JSON. Also, when dereferencing an object nested in an array, you must remember your array indexing. The first two layers of your JSON object have array values. The correct formulation is: var txt = '{"tblCommoHier":[ {"DEPT":[' + '{"DEPT":"100","DEPT_NAME":"Collectibles" },' + '{"DEPT":"105","DEPT_NAME":"Commodities" },' + '{"DEPT":"140","DEPT_NAME":"Souvenir" }]}]}';
var obj = JSON.parse(txt);
var elem = obj.tblCommoHier[0].DEPT[0].DEPT;
|
Count the length of JSON array and access the json element in PHP
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Have you tried a different url/file? I coppied your code exact and it works 100%. Make sure the php://input is correct. Try store the JSON in an external file and try link to that file. $json = file_get_contents('something.json');
$arr = json_decode($json, true);
$count = 0;
for($i = 0; $i < count($arr['data']); $i++) {
$count++;
}
echo $count;
|
How do I access an element of this JSON object created by JSON.parse?
Date : March 29 2020, 07:55 AM
will help you This very same issue happened to me when I mistakenly used JSON.stringify on an object that was already a string, which it looks is exactly what you are doing.
|
how to access a JSON element when i have a value of another element in the same object?
Date : March 29 2020, 07:55 AM
seems to work fine You can use Array.prototype.find to search for a specific property in an array of objects. var person=[{"name":"joe","age":21,"class":"a"},{"name":"moe","age":22,"class":"b"}];
const joe = person.find(item => item.name === 'joe');
if(joe)
console.log(`Joe is ${joe.age} years old`);
|