Not getting return values in $getJSON object - javascript

Javascript:
$.getJSON("path", function(data){alert(data.name)})
Guys in this line "data" is not working as an object. name is unrecognizable here.
I am sending multiple outputs in php using json_encode() function but not able to get those values in javascript file.
PHP Code:
$book = array("name"=>"bob", "num"=>1);
echo json_encode($book);

Related

How to search Steam game API JSON output?

I am working with following APIs:
http://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json
https://store.steampowered.com/api/appdetails?appids=GAMEID
(gameid example: "730" - counter strike)
My goal with both of these is to search them for data. For example I want the first API to give me a name of a game based on it's ID and the second one to give me specific information about a game for example if it has trading cards (id:29 in the API).
I tried a few things but I am kinda lost on this beacuse I don't really understand JSON so I would really appreciate some help.
I am open to both PHP and JS solutions.
IN PHP, you can use the json_decode() function to convert JSON data into an array. You can them access the values as you do for a classic array :
$appID = 730 ;
$url = 'https://store.steampowered.com/api/appdetails?appids=' . $appID ;
$content = file_get_contents($url) ; // retrieve the JSON data string from the website
$data = json_decode($content, true); // convert the JSON string into PHP array
echo $data[$appID]['data']['name'] ; // Counter-Strike: Global Offensive
var_dump($data[$appID]['data']['is_free']); // bool(true)

How to show data from JSON string converted array

I want to show some data from JSON string by making that string to array
I have used json_decode to convert the json string into an array. Here is my json string (dd):
"{"title":"W3Schools Online Web Tutorials","description":"w3schools.com","image":"http:\/\/www.w3schools.com\/images\/colorpicker.png","url":"https:\/\/www.w3schools.com\/"}"
When I am returning the array or dd the array, that showing me the array as I intended (dd):
array:4 [▼"title" => "W3Schools Online Web Tutorials""description" => "w3schools.com""image" => "http://www.w3schools.com/images/colorpicker.png""url" => "https://www.w3schools.com/"]
But then when I am trying to show $myarray->title it is giving me error:
Trying to get property 'title' of non-object
public function showDetail(Request $request){
$rUrl = "http://api.linkpreview.net/?key=5c59318d927ca5c5b481c89a6c18a0a2623a61d568502&q=".$request->body;
$json_string= file_get_contents($rUrl);
$data= json_decode($json_string,true);
return view('showIn')->with('data', $data);
}
Expected result: W3Schools Online Web Tutorials
Actual result: Error:Trying to get property 'title' of non-object
Your JSON string is in double quotes for one, use single quotes.
Secondly, $myarray->title is what you would do if you were working with an object. Since your working with an array, do it like $myarray['title']
This will work.
$myjson = '{"title": "W3Schools Online Web Tutorials", "description":"w3schools.com","image":"http:\/\/www.w3schools.com\/images\/colorpicker.png","url":"https:\/\/www.w3schools.com\/"}';
$myarray = json_decode($myjson, true);
echo $myarray['title'];
It is an array so you should be able to access it like this $data['title'];
You should use response json instead of return view('showIn')->with('data', $data);
return response()->json($data);
but if you want to print json in your view you can simply do
$rUrl = "http://api.linkpreview.net/?key=5c59318d927ca5c5b481c89a6c18a0a2623a61d568502&q=".$request->body;
$json_string= file_get_contents($rUrl);
return view('showIn')->with('json', $json_string);
In your blade template use
{{ $json }}

How to read a JSON Array in php without any key?

I have a JSON formed #Javascript and passed to PHP via AJAX , problem is that I cannot assign keys to each JSON Object inside the JSON Array .
The JSON Array looks something like this :
[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]
,Now , in PHP ,how to iterate the data , I am a newbie to PHP , so please excuse me if I am asking a very silly question.
The link you provided has examples with key, mine is without key.
Thanks
$json = '[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]';
$array = json_decode($json,true);
foreach($array as $item) {
echo $item['jobId']."-".$item['cname']."-".$item['removal_id']."-".$item['quantity']."\n";
}
Output
90-Subhasish-101-3
90-Subhasish-102-2
90-Subhasish-103-4
90-Subhasish-104-4
90-Subhasish-105-0
90-Subhasish-106-5
90-Subhasish-107-0
90-Subhasish-108-0
90-Subhasish-109-4
Example
Using json_decode($json_object).
$json = json_decode($json_object);
foreach($json as $j){
print_r($j)
}

How can I pass foreign characters (e.g. æ) to a javascript array from a PHP array?

I have an array of file paths, which is stored in a PHP array called $files, which I would like to pass to a javascript array using this command:
var files = <?php echo json_encode($files)?>;
Which is fine, however it doesn't pass any values which have special characters for example one that contains 'æ'. Instead that value in Javascript comes out as null. Is there another way of preserving these characters? The values are all file paths, which seem to be passed over OK using json_encode, just not these special characters.
I've solved it. I essentially ran through the array using url_encode, and THEN JSON'd that for Javascript to interpret.
$filesEncoded = array();
foreach ($files as $f) {
array_push($filesEncoded,urlencode($f));
}
<script>
var files = <?php echo json_encode($filesEncoded)?>;
</script>

Populate PHP Array Values in Javascript

Here is my code:
I am getting data from mysql query and getting array of values. But i am not able to populate in javasript. could you please any one help me out...
PHP Code:
$parishFamilyInfo = $parishFamilyInfoBO->sendBirthdayGreeting($personalInfoSearchDO, $parishFamilyInfoSearchDO);
foreach ($parishFamilyInfo as $parishFamily){
if(!empty($parishFamily->email)){
print_r($parishFamily);
}
}
JavaScript:
$(document).ready(function(){
var t = $('#people').bootstrapTransfer(
{'target_id': 'multi-select-input',
'height': '15em',
'hilite_selection': true});
t.populate([ /*here i need to populate php array value*/
{
value:"<?php print_r($parishFamily->email);?>",
content:"<?php print_r($parishFamily->name);?>"
},
]);
//t.set_values(["2", "4"]);
//console.log(t.get_values());
});
print_r is only for debugging. It's not for anything else. Let me quote the documentation:
print_r — Prints human-readable information about a variable
As stated above, it is a debugging function that's used to print the contents of a variable in a more readable manner. It just returns a string — JavaScript does not and will not understand this format.
Use JSON (JavaScript Object Notation) instead. It is is a light-weight data interchange format that is very popular nowadays. You can use the built-in function json_encode to create the JSON representation of your PHP array and then transfer it to JavaScript. JSON being language independent and based on the JavaScript scripting language, your JavaScript code will now be able to parse the array:
t.populate([
{
value: <?php echo json_encode($parishFamily->email); ?>,
content: <?php echo json_encode($parishFamily->name); ?>
},
]);

Categories