I'm trying to parse a JSON string from a url with a named array.
Ex.
{
"Attendees": [{
"user_id": "123",
"first_name": "Jim",
"last_name": "Bill",
"email": "jsfbsjkd#aim.com",
"title": "Admin",
"business_unit": "",
"area": ""
}]
}
i've tired this:
$(document).ready(function(){
$.getJSON('http://localhost/testJSON/json.php', function(data){
alert(data.user_id); // 1 });
});
but it's creating Object Object. Any help is appreciated.
Instead of alert(), try this:
console.log(data.Attendees[0].user_id);
Then using developer tools in Chrome (or another developer friendly browser), check the console for the result here.
Related
for example, i have json result like this
[
{
"title": "x1",
"url": "domain.com"
},
{
"title": "x2",
"url": "example.com/"
},
{
"title": "x3",
"url": "site.com/cam"
},
]
The result of json will be randomly, i want to scrape ['x']['url'] path from that json, but the value of ['url'] must be "site.com/cam", and as you know, the results from json will change randomly, so I do not know which json path with value is "site.com/cam".
is there any suggestion? Thank you
Try this
$index = array_search('site.com/cam',array_column(json_decode($json),'url'));
Sandbox
https://3v4l.org/72tsZ
Output
$index = 2
Oh and there is a typo in your JSON
}, <-- remove comma here.
]
I'm using PHP to fetch JSON stored in a MySQL database, put it into a PHP variable, and pass it to JQuery, which writes the content to the page:
data = $.parseJSON('<?=$my_json;?>');
$.each(data.links, function(entryIndex, entry) {
//do some stuff here
});
Everything works fine when the JSON looks like this:
{
"links": [{
"url": "http://domain1.com",
"title": "Title 1",
"description": "This is an example for my question on Stack Overflow"
}, {
"url": "http://domain2.com",
"title": "Title 2",
"description": "This is another example for my question on Stack Overflow"
}, {
"url": "http://domain3.com",
"title": "Title 3",
"description": "This is a third example for my question on Stack Overflow"
}]
}
But when the JSON content includes quotes, I get an error reading "Uncaught SyntaxError: Unexpected end of JSON input," even though the quotes are escaped, like this:
{
"links": [{
"url": "http://domain1.com",
"title": "Title 1",
"description": "This is an \"example\" for my question on Stack Overflow"
}, {
"url": "http://domain2.com",
"title": "Title 2",
"description": "This is another \"example\" for my question on Stack Overflow"
}, {
"url": "http://domain3.com",
"title": "Title 3",
"description": "This is a third \"example\" for my question on Stack Overflow"
}]
}
What am I doing wrong?
data = $.parseJSON('<?=$my_json;?>');
The thing you're doing here is you're trying to wrap up the JavaScript object around quotes and passing it as a string to $.parseJSON function.
This function only parses JSON string which is a string should be formed with JSON structure and that will then converted to a JavaScript object by the function.
While $my_json variable already has a JavaScript object, you don't need to use $.parseJSON function. You can just assign it to the JavaScript variable like the following one:
data = <?= $my_json ?>;
$.each(data.links, function(entryIndex, entry) {
//do some stuff here
});
From MySQL row, I have this json formatted data:
$row['details'] =
{
"previous_employer":[
{"employer":"string1","address":"address1"},
{"employer":"string2","address":"address2"},
{"employer":"string3","address":"address3"}],
"profile":[
"firstname":"John",
"lastname":"Adams",
"gender":"male",
"age":"35",
"contact":"123456789"]
}
I want to extract employer and address on the previous_employer array of objects,
but when I do this:
$json = json_decode($row['details'],true); //decode into array
foreach($json['previous_employer'] as $d){
echo "employer:".$d['employer']."<br>address:". $d['address']."<br>";
}
it gives me an error of
Warning: Invalid argument supplied for foreach()
How can I fix this? Pls advise.. thanks!
You JSON is invalid, "profile" must be
An Object:
"profile": {
"firstname": "John",
"lastname": "Adams",
"gender": "male",
"age": "35",
"contact": "123456789"
}
or an Array of Object (here his length == 1)
"profile": [{
"John",
"Adams",
"male",
"35",
"123456789"
}]
or a Simple Array (not associative array/map)
"profile": [
"John",
"Adams",
"male",
"35",
"123456789"
]
Now, your posted code will work as a charm without any modifications ... :)
json_decode() does not necessarily succeed (just imagine you feed it with complete garbage, why should it return something?). You need to do the following error checking:
Verify its return value:
Returns the value encoded in json in appropriate PHP type. Values
true, false and null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot be decoded or if the
encoded data is deeper than the recursion limit.
If valid data can be expected to return null some times, call json_last_error() and check whether it's JSON_ERROR_NONE.
Last but not least, you can explore any variable with var_dump(). You don't need to make assumptions about its content.
I have passed JSON encoded parameters by POST which we have captured and decoded in another PHP file. I have used the following code to do that.
$entityBody = file_get_contents('php://input');
$entityBody = json_decode($entityBody, true);
I have passed the JSON encoded parameters as follows:
{
"id": "5",
"name": "abcd",
"imei": "1234"
}
Here my code works perfectly fine. However, I want to get all the parameters into a single object so that we can store them efficiently because otherwise there will be too many ifs and elses to get each parameter. So I have encoded the parameters as follows:
device = {
"id": "5",
"name": "abcd",
"imei": "1234"
}
But it is not working. Being new to JSON and PHP I do not know how to handle such cases. How can I achieve this?
use json_decode($_POST['device'], true) since your actually passing a parameter called 'device' to the php file.
You should pass json objects as follow:
{"device" : {
"id": "5",
"name": "abcd",
"imei": "1234"
}}
or if you have an array of devices
{"device" : [{
"id": "5",
"name": "abcd",
"imei": "1234"}
]}
I am loading in a JSON feed from Facebook (snippet below).
{
"data": [
{
"id": "115972604762",
"from": {
"name": "Title Here",
"category": "Musicians",
"id": "20274769762"
},
"name": "It was an amazing gig!!",
"picture": "http://photos-h.ak.fbcdn.net/hphotos-ak-snc1/hs196.snc1/6616_115972604762_20274769762_2185148_6347071_s.jpg",
"source": "http://sphotos.ak.fbcdn.net/hphotos-ak-snc1/hs196.snc1/6616_115972604762_20274769762_2185148_6347071_n.jpg",
"height": 453,
"width": 604,
"images": [
{
I am loading it in using $data['pics'] = json_decode(file_get_contents('https://graph.facebook.com/'. $id .'/photos'));
How would I go about echo'ing out the from->name value to get the 'Title Here' value?
I think it should just be this:
$array = json_decode(file_get_contents('https://graph.facebook.com/'. $id .'/photos'));
echo $array["data"]["from"]["name"];
You can echo out the array using print_r($array) and then see the structure of your php array if it didn't work as expected.
First thing I would do is var_dump() the response, that would explain the exact structure of how PHP has decoded it. My guess is that $response['data']['from']['name'] might work.