I'm trying to access stock Quotes through Google Finance
by doing so:
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$json = str_replace("\n", "", $quote);
$data = substr($json, 4, strlen($json) -5);
print_r($data);
$json_output = json_decode($data, true);
print_r($json_output);
echo "\n".$json_output['l'];
json_decode suppose to give me an normal array with keys and values, but it doesn't.
If you look at json_last_error() after attempting json_decode() you'll see that you are getting:
JSON_ERROR_UTF8 Malformed UTF-8 characters, possibly incorrectly encoded
Try this:
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$json = substr($quote, 4, -5);
$json_output = json_decode($json, true, JSON_UNESCAPED_UNICODE);
print_r($json_output);
See: http://3v4l.org/jkInl
Note that JSON_UNESCAPED_UNICODE is only available as of php 5.4
Another option would be to do...
$quote = utf8_decode($quote);
...after you fetch it. This will convert the euro symbol into a ? character. Might not be what you want, but at least you get json_decode to return an array to you.
Update: See here for more information:
PHP decoding and encoding json with unicode characters
Another solution is to "manually" replace all instances of € by \u20AC (which is the unicode character point for the Euro sign).
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=VSE:APG1L');
$quote = str_replace(chr(128), '\u20AC', $quote);
$json = substr($quote, 6, -3); // remove unnecessary '// [ … ]'
$json_output = json_decode($json, true);
print_r($json_output);
Related
I have data set smiller to dictonary and I want to get key value pair for this data.
here is DATA SET:
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
I am doing json_decode($data, true); but have no luck with it
Sorry If I am unclear.f
BTW I am doing it in PHP
the result should be like this:
test_field2: NONE
test_field3: NONE
Since your data is invalid json because of that u in it here is a solution
json_decode(str_replace("'",'"',str_replace("u'","'",$data)), true);
Should do the trick
You've to try like this because on your existing code couple of issue exists.
Replace Unnecessary character u' with '
Replace single quotes(') on json string. For json double quotes(") is permitted.
So just search these two characters and replace with '' and " respectively.
<?php
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}";
$search = ["u'","'"];
$replace = ["'",'"'];
$without_u = str_replace($search,$replace,$data);
$array = json_decode($without_u, true);
print '<pre>';
print_r($array);
print '</pre>';
?>
DEMO: https://eval.in/1032316
I am trying to retreive the current stock price from Google Finance:
http://finance.google.com/finance/info?client=ig&q=AAPL
How can I extract just the price ("1")?
<?php
$string = file_get_contents("http://finance.google.com/finance/info?client=ig&q=AAPL");
$json = json_decode($string, true);
$price = $json["1"];
echo $price;
?>
the json returned is commented out, therefore json_decode() won't do the business... you need to remove the double slashes - I used explode() like this:
<?php
$string = file_get_contents("http://finance.google.com/finance/info?client=ig&q=AAPL");
$arrMatches = explode('// ', $string); // get uncommented json string
$arrJson = json_decode($arrMatches[1], true)[0]; // decode json
$price = $assJson["l"];
echo $price;
oh, and the key is a lowercase L (l) not a numeric one (1) in the json
The "$arrJson = json_decode($arrMatches[1], true)[0];" line doesn't work because "$arrJson" is empty after it has been executed.
Why is my string converted to UNICODE in JSON?
http://example.com/test/test.php?MyComment=%C4%93
Saved in JSON as {"MyComment":"\u0113 "}
I want to be saved as {"MyComment":"%C4%93"}
PHP:
$MyComment = $_GET["MyComment"];
print_r($MyComment); //%C4%93
$results = array ( array(
"MyComment" => $MyComment,
));
$inp = file_get_contents('Test.json');
$arr = json_decode($inp);
$results = array_merge($results, $arr);
$fp_login = fopen('Test.json', w);
fwrite($fp_login, json_encode($results));
fclose($fp_login);
If you just want to have the urlencoded value, you have to replace this:
$MyComment = $_GET["MyComment"];
with this (updated due to the comments below):
$MyComment = urlencode( $_GET["MyComment"] );
For your Swift problem you should use :
$MyComment = rawurlencode( $_GET["MyComment"] );
The whitespace will be encoded as %20 so you can decode it at the Swift side.
console output:
Object {MyComment: "%C4%93"}
You have to pass the constant JSON_UNESCAPED_UNICODE to avoid it.
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
In php 5.4+, php's json_encode does have the JSON_UNESCAPED_UNICODE option for plain output. On older php versions, you can roll out your own JSON encoder that does not encode non-ASCII characters.
I'm trying to use a few values from this array that comes out of json_decode, but can't get any output from var_dump (which would presumably help me figure out how to access a particular value). I've tried $data[0] also from a suggestion on a similar question.
<?PHP
$json = 'callback({"geobytesinternet":"US","geobytescountry":"United States","geobytesregionlocationcode":"USWI","geobytesregion":"Wisconsin","geobytescode":"WI","geobyteslocationcode":"USWILOLA","geobytescity":"Land O Lakes","geobytescityid":"30028","geobytesfqcn":"Land O Lakes, WI, United States","geobyteslatitude":"46.154598","geobyteslongitude":"-89.396896","geobytescapital":"Washington, DC ","geobytestimezone":"-89.3969","geobytesnationalitysingular":"American","geobytespopulation":"278058881","geobytesnationalityplural":"Americans","geobytesmapreference":"North America ","geobytescurrency":"US Dollar","geobytescurrencycode":"USD","geobytestitle":"The United States"});';
$json = str_replace( 'callback(', '', $json);
$json = str_replace( ');', '', $json);
$json = '[' . $json . ']';
$data = json_decode($json);
echo '<br>decoded: ' . $data;
echo '<br><br>var_dump: ' . var_dump($data[0]);
?>
The decoded data is object array so do something like this:
$result = $data[0];
echo $result->geobytesinternet;
You are almost there, the function json_decode() takes as input json encoded string. Your string is not properly json encoded, because of this line $json = '[' . $json . ']';.
This is properly encoded json string
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. See the documentation.
EDIT:
So your code should look something like this:
<?php
$json = 'callback({"geobytesinternet":"US","geobytescountry":"United States","geobytesregionlocationcode":"USWI","geobytesregion":"Wisconsin","geobytescode":"WI","geobyteslocationcode":"USWILOLA","geobytescity":"Land O Lakes","geobytescityid":"30028","geobytesfqcn":"Land O Lakes, WI, United States","geobyteslatitude":"46.154598","geobyteslongitude":"-89.396896","geobytescapital":"Washington, DC ","geobytestimezone":"-89.3969","geobytesnationalitysingular":"American","geobytespopulation":"278058881","geobytesnationalityplural":"Americans","geobytesmapreference":"North America ","geobytescurrency":"US Dollar","geobytescurrencycode":"USD","geobytestitle":"The United States"});';
//Compact a bit the code, note the ');' replacement
//str_replace() can take an array of arguments, so no need to call the function multiple times
$json = str_replace( array('callback(', ');'), '', $json);
//Get elements in a array
$data = json_decode($json);
//See what's inside the array
var_dump($data);
?>
This is a working fiddle.
The examples I checked seems simple but I can't figure it out what I'm doing wrong.
I'm trying to convert and display JSON text as object array in PHP.
$json = '{"color1":red, "color2":blue, "color3":yellow}';
$arr= json_decode($json, true);
print_r($arr);
It doesn't output anything. but when I print $json, the output is just fine
Well that's not json, try
$json = '{"color1":"red", "color2":"blue", "color3":"yellow"}';
$arr= json_decode($json, true);
print_r($arr);
notice the strings are quoted with ", also see http://json.org