PHP: having trouble with JSON and reading into Python - php

I'm trying to get an array into JSON so I can read it out correctly into a Python script. I've managed to get the URL to output data but I'm not sure that it's putting out the correct format. I'm getting an 'Invalid encoding, expecting UTF-8, UTF-16 or UTF-32.' error when I run it through a JSON Parser.
Here's the result that I'm getting from the URL....
[
{
"year":"2016",
"Month":"Apr",
"the_days":"16, 29, 30"
},
{
"year":"2016",
"Month":"May",
"the_days":"13, 27"
},
{
"year":"2016",
"Month":"Jun",
"the_days":"10, 11, 24"
},
{
"year":"2016",
"Month":"Jul",
"the_days":"08, 22, 23"
},
{
"year":"2016",
"Month":"Aug",
"the_days":"06, 20"
},
{
"year":"2016",
"Month":"Sep",
"the_days":"02, 03, 16, 17, 30"
},
{
"year":"2016",
"Month":"Oct",
"the_days":"01, 14, 15, 29"
},
{
"year":"2016",
"Month":"Nov",
"the_days":"25"
},
{
"year":"2016",
"Month":"Dec",
"the_days":"09, 10, 23, 24"
}
]
This is achieved by putting the data into an array like this....
$encodeArray = array();
while($row = $result->fetch_assoc()) {
$encodeArray[] = $row;
}
header("Content-Type: application/json; charset=utf-8");
I'm having trouble figuring out what's wrong with this - could do with some help!

Use json_encode function to convert your array to json string in php and print it on page using echo statement. So your script can get response
<?php
$json_arr = array();
while($row = $result->fetch_assoc()) {
array_push($json_arr,$row);
}
echo json_encode($json_arr);
?>

It's looking more and more like there is nothing wrong with my array - it's possible that the problem is happening in my Python script that won't decode the json.
Separate question to follow!

Related

fail in adding numeric values to characters on speical characters

I have this function:
function dec($dec)
{
$ans="";
for($x=0; $x<strlen($dec);$x++)
{
$tempAscii = ord(substr($dec,$x,1)) - ($x+1)*2; //((int)dec[x] - (x+1)*2)
while ($tempAscii <0) $tempAscii+=256;
echo $tempAscii,", ";
$ans = $ans . chr($tempAscii);
}
return $ans;
}
echo "dec, ", dec($_GET['word']);
for example, those dec results are just fine:
input: as output: _o middle output: 95, 111
input: SHA256 output: QD;*+* middle output: 81, 68, 59, 42, 43, 42
But when I'm trying this input, I got something strange:
input: $#SHA256*!# output: " middle output: 34, 60, 77, 64, 55, 38, 39, 38, 24, 13, 13
I'm not sure if the reason I get this output is because I'm doing a mistake in my function, or there is other reason. Why I got " as the last result? thanks.
EDIT: I'm using firefox to see the results.
EDIT: It seems that <is the problem. It seems that firefox take this as "smaller then"... How can I change it to be just > char without any meaning?
Here is the solution:
as #kyeiti mention before, the browser trying to interpret this string as code.
according to this question I solved this by using the command htmlspecialchars. So now my code is:
echo htmlspecialchars(enc($_GET['word']));
many thanks!

I want to sort arrays, but I can't?

I have a JSON file that I created, and I want to sort from the biggest price value.
JSON is like this:
[
{
"username": "elbugato",
"sold": 19,
"qmimi": 38.5
},
{
"username": "Pablo",
"sold": 12,
"qmimi": 42
},
{
"username": "Hqstuff",
"sold": 0,
"qmimi": "0"
},
{
"username": "matchspamm3rs",
"sold": 0,
"qmimi": "0"
},
{
"username": "Pachenko",
"sold": 1,
"qmimi": 1.1
},
I want to sort qmimi from the highest value
My php code is this.
$sellertop8json = json_decode(get_html('link'));
$i = 1;
sort($sellertop8json->qmimi, SORT_NUMERIC);
foreach($sellertop8json as $top8){
max($top8);
if (++$i == 8) break;
echo '<tr>
<td>'.$top8->username.'</td>
<td># '.$top8->sold.'</td>
<td>$ '.$top8->qmimi.'</td>
</tr>
';
}
but they aren't sorting from the biggest value
The results I get :
Look at "Pachenko", he is after a seller that has "0" Earned.
Thank You
Sorry for my bad English
P.S : JSON ISN'T RAW, I copied from some extension I am using on google chrome, so JSON it's not the problem.
You need to use usort and provide custom function comparing the field of the element:
usort($sellertop8json, function($a, $b) {
return $a->qmimi == $b->qmimi ? 0 :
$a->qmimi < $b->qmimi ? 1 : -1;
}
);
The comparison function must return 0 if the elements are equal, less than 0 if the first element is lower and greater than 0 if the second element is higher.
This may be a bit confusing as you're sorting from highest and therefore the swap of the sign in comparison - see the last part of comparison.

Issue in JSON Decode

Hi I am getting a weird issue in JSON decode, JSON is getting decoded correctly locally, but on my server the json_decode function returns NULL.
This is the JSON that I am posting from my test page:
[
{
"pictureTaken": 0,
"unit_id": 20192,
"id": 2,
"deficiency_id": 155,
"last_modifier_id": 4,
"comments": "Living room",
"level": 3,
"location": "Living room",
"property_id": 26,
"inspectable_item_id": 44,
"building_id": -769876698
}
]
now when I do var_dump(json_deocde($_POST['data'], true)); I get NULL response.
when I do echo $_POST['data']; I get:
[ { \"pictureTaken\": 0, \"unit_id\": 20192, \"id\": 2, \"deficiency_id\": 155, \"last_modifier_id\": 4, \"comments\": \"Living room\", \"level\": 3, \"location\": \"Living room\", \"property_id\": 26, \"inspectable_item_id\": 44, \"building_id\": -769876698 } ]
I think due to these \" json_decode is not working, kindly help me in fixing this issue,
Some Server Info:
PHP Version 5.2.17
json version 1.2.1
You have magic quotes enabled on your server. Disable them.
you can always do this :
var_dump(json_deocde(str_replace("\",$_POST['data']), true));
that would remove the slashes from your json string

How would I convert this array to usable javascript array?

I have php code that returns this from a database,
[
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:25:37",
"NumPlayers": "2"
},
{
"Time": "2012-11-27 16:29:33",
"NumPlayers": "2"
The times are MySQL timestamps. I need to get it into this format to use with Highcharts javascript charting.
data: [
[Date.UTC(1970, 9, 9), 0 ],
[Date.UTC(1970, 9, 14), 0.15],
[Date.UTC(1970, 10, 28), 0.35],
[Date.UTC(1970, 11, 12), 0.46],
I'm having trouble figuring out how to loop through the MySQL results and convert the timestamps to javascript Date.UTC objects. I need to place the NumPlayers value after the Date.UTC objects, and output it in the format below. I'm a PHP and javascript noob :\
It should look something like this:
data: [
[Date.UTC(2012, 11, 27, 16, 10, 35), 1],
[Date.UTC(2012, 11, 27, 16, 24, 55), 1],
[Date.UTC(2012, 11, 27, 16, 25, 37), 2],
[Date.UTC(2012, 11, 27, 16, 29, 33), 2],
You should realize that Date.UTC(2012, 11, 27, 16, 10, 35) simply returns the number of milliseconds since the epoch (1356624635000). Therefore, you can just convert your object into UNIX timestamps (times a 1000 since JS works with millisecond timestamps, but PHP works with seconds).
Sample Code
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
// Make sure date is parsed as UTC
date_default_timezone_set("UTC");
// Convert items into the desired format
$mapper = function($item) {
return array(strtotime($item->Time)*1000, $item->NumPlayers);
}
echo json_encode(array_map($mapper, json_decode($data)));
Output
[[1354032635000,"1"],[1354033495000,"1"]]
You seem to be getting straight JSON from your database, which you can always convert into an array:
$data = '[{"Time": "2012-11-27 16:10:35", "NumPlayers": "1"}, {"Time": "2012-11-27 16:24:55", "NumPlayers": "1"}]';
$arrayData = json_decode($data, true);
After which you can simply iterate through the array and print out the contents of the array in the JS format you need. Something like that:
echo 'data: [' . PHP_EOL;
foreach ($arrayData as $item) {
echo '[Date.UTC(';
$tmp = preg_split( '/(-| |\:)/', $item['Time'] );
echo implode(', ', $tmp);
echo '), ' . $item['NumPlayers'] . '],';
echo PHP_EOL;
}
echo PHP_EOL . '];';
You can split the time string into sections using /[^\d]+/g:
var finalData = [], numbers=/[^\d]+/g, item, dateParts, newDate;
for (var i=0, l=data.length; i<l; i++) {
item = data[i];
dateParts = item.Time.split(numbers);
newDate = Date.UTC.apply(Date, dateParts);
finalData.push([newDate, item.NumPlayers]);
}
See also: MDN's documentation on JavaScript regular expressions (/[^\d]/g is a regex).
The best way to provide data to Highcharts is getting data formated from database, so that you don't have to do it on client side.
To do it you just have to change your query to something like the following.
Backend
$query = "SELECT UNIX_TIMESTAMP(date) * 1000 AS 'date', value FROM ...";
Then to send the result to frontend:
echo json_encode($result);
Frontend
To get the result on frontend, this case using jQuery and assuming that url is your url:
$.getJSON(url, function(json) {
// decode json
var data = $.parseJSON(json);
// Then you just have to pass it to your series
});
Or better, store date values in UTC.
var php_data = [
{
"Time": "2012-11-27 16:10:35",
"NumPlayers": "1"
},
{
"Time": "2012-11-27 16:24:55",
"NumPlayers": "1"
}
];
var length = php_data.length;
hc_data = [];
for(var i = 0; i< length; i++){
php_date = new Date(php_data[i]["Time"]);
hc_data.push(
[
Date.UTC(
php_date.getFullYear(),
php_date.getMonth() + 1,
php_date.getDate()
),
php_data[i]["NumPlayers"]
]
);
}
// hc_data is your Array

PHP reading invalid json with json_decode();

I have invalid external json data, without double quotes around names.
Example:
{
data: [
{
idx: 0,
id: "0",
url: "http://247wallst.com/",
a: [
{
t: "Title",
u: "http://247wallst.com/2012/07/30/",
sp: "About"
}
],
doc_id: "9386093612452939480"
},
{
idx: 1,
id: "-1"
}
],
results_per_page: 10,
total_number_of_news: 76,
news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
result_start_num: 2,
result_end_num: 2,
result_total_articles: 76
}
As you see a lot of names like data,idx,id,url and others are not double quoted, so this makes this json invalid.
How can I make this external json valid? I already tried str_replace, replacing '{' to '{"' and ':' to '":' adding double quotes around unquoted names, but this messes up some already double quoted variables.
How can I make this json valid so I can read this data with PHP json_decode? I'm not very familiar with preg_replace..
Valid json will look like:
{
"data": [
{
"idx": 0,
"id": "0",
"url": "http://247wallst.com/",
"a": [
{
"t": "Title",
"u": "http://247wallst.com/2012/07/30/",
"sp": "About"
}
],
"doc_id": "9386093612452939480"
},
{
"idx": 1,
"id": "-1"
}
],
"results_per_page": 10,
"total_number_of_news": 76,
"news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
"result_start_num": 2,
"result_end_num": 2,
"result_total_articles": 76
}
Please suggest me some php preg_replace function.
Data source:
http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1
With preg_replace you can do:
json_decode(preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $in));
Since the above example won't work with real data (the battle plans seldom survive first contact with the enemy) heres my second take:
$infile = 'http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1';
// first, get rid of the \x26 and other encoded bytes.
$in = preg_replace_callback('/\\\x([0-9A-F]{2})/i',
function($match){
return chr(intval($match[1], 16));
}, file_get_contents($infile));
$out = $in;
// find key candidates
preg_match_all('#(?<=\{|\[|,)\s*(?<key>(?:\w|_)+?)\s*:#im', $in, $m, PREG_OFFSET_CAPTURE);
$replaces_so_far = 0;
// check each candidate if its in a quoted string or not
foreach ($m['key'] as $match) {
$position = $match[1] + ($replaces_so_far * 2); // every time you expand one key, offsets need to be shifted with 2 (for the two " chars)
$key = $match[0];
$quotes_before = preg_match_all('/(?<!\\\)"/', substr($out, 0, $position), $m2);
if ($quotes_before % 2) { // not even number of not-escaped quotes, we are in quotes, ignore candidate
continue;
}
$out = substr_replace($out, '"'.$key.'"', $position, strlen($key));
++$replaces_so_far;
}
var_export(json_decode($out, true));
But since google offers this data in RSS feed, i would recommend you to use that one if it works for your usecase, this is just for fun (-:
The JSON feeds from Google always seem to be plagued with problems- formatted incorrectly in some way shape or form. If you switch the feed to RSS you can easily convert it to an array or JSON from the array.
<?php
$contents = file_get_contents('http://www.google.com/finance/company_news?q=aapl&output=rss&start=1&num=1');
// Convert the RSS to an array (probably just use this)
$arr = simplexml_load_string($contents);
// Or if you specifically want JSON
$json = json_encode($arr);
// And back to an array
print_r(json_decode($json));

Categories