Gossamer Forum
Quote Reply
JSON
Has anyone worked out how to get GLinks to produce JSON?
I'm finding some difficulties adding the right ['s and {'s etc (basically, 'enclosing' sublists in those - how the heck?)
Something with <%first%> and <%last%> in loops?
Any ideas?

The below is an example of JSON:
Code:
var json = {
"name": "Categories",
"id": "root",
"children": [
{
"name": "Cat1",
"children": [
{
"name": "Cat1.1",
"children": [
{
"name": "Cat1.1.1",
"children": [
{
"name": "Cat1.1.1.1",
"data": {
"$color": "#21ff59"
}
}, {
"name": "Cat1.1.1.2",
"data": {
"$color": "#21ff59"
}
}, {
"name": "Cat1.1.1.3",
"children": [
{
"name": "Cat1.1.1.3.1",
"data": {
"$color": "#21ffc8"
}
}, {
"name": "Cat1.1.1.3.2",
"data": {
"$color": "#21ffc8"
}
}, {
"name": "Cat1.1.1.3.3",
"data": {
"$color": "#21ffc8"
}
}, {
"name": "Cat1.1.1.3.4",
"data": {
"$color": "#21ffc8"
}
}
],
"data": {
"$color": "#21ff59"
}
}, {
"name": "Cat1.1.1.4",
"data": {
"$color": "#21ff59"
}
}, {
"name": "Cat1.1.1.5",
"children": [
{
"name": "Cat1.1.1.5.1",
"children": [
{
"name": "Cat1.1.1.5.1.1",
"data": {
"$color": "#21c8ff"
}
}, { ......
Quote Reply
Re: [gotze] JSON In reply to
So you are trying to make a list of all the categories into an array? How is this being called?

Personally, seems a bit of a slow way to do it (I would opt for using AJAX to send a request to a script to get the category list, and then return that contents back to the browser)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [gotze] JSON In reply to
Hi John,

With recent updates, GT has put in this kind of module. It is GT::JSON. Below are some lines from its doc.
Code:
use GT::JSON; # imports encode_json, decode_json, to_json and from_json.
$json_text = to_json($perl_scalar);

But I am not sure if that solves your problems.

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] JSON In reply to
Hi Dat,

I knew GT did some work on JSON, but only just saw the GT::JSON module, which looks perfect. If only there were one or two examples of its use ...
Quote Reply
Re: [gotze] JSON In reply to
I love GT::JSON Smile

Here is a global that produces a list of categories in JSON:

Code:
sub {
use GT::JSON;
my $category = $DB->table('Category');
$category->select_options('ORDER BY Full_Name');
my $sth = $category->select(['Has_New_Links','Newest_Link','Number_of_Links','ID','FatherID','Name','Full_Name'] );
my @cats;
while (my $cat = $sth->fetchrow_hashref) {
$cat->{URL} = "$CFG->{build_root_url}/" . $category->as_url($cat->{Full_Name}) . "/$CFG->{build_index}";
push @cats, $cat;
}
encode_json { categories_json => \@cats };
}

Example:
http://slashdemocracy.org/...ge.cgi?page=CatsJSON
Quote Reply
Re: [gotze] JSON In reply to
Of course, I'd still love to see examples of JSON usage.
Quote Reply
Re: [gotze] JSON In reply to
Hi John,
I found out, that json is something like xml.
But what will you actually do with the json categories?

Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] JSON In reply to
Hi Matthias,

Yes, JSON is 'fat free XML', see http://www.json.org/fatfree.html.

One application of JSON (and AJAX) is Autosuggest (try search box at http://slashdemocracy.org/links/), and see http://www.gossamer-threads.com/...le_Suggest)_P275691/

Today, I was looking at http://eakatalog.com/...Icicle/example2.html and wanted to try that with GLinks ...

Generally, I think JSON (and XML) is particularly relevant for making and open API for GLinks.
Quote Reply
Re: [gotze] JSON In reply to
Hi John,
thanks for your explanation.
Did you change your autosuggest function on your page?
I thougt you are using this code
http://www.gossamer-threads.com/forum/Products_C9/Gossamer_Links_C5/Development%2C_Plugins_and_Globals_F20/Re%3A_%5Bpugdog%5D_Suggest_query_(as_in_Google_Suggest)_P281484/#p281484

Matthias
gpaed.de

Last edited by:

Matthias70: Jul 26, 2010, 9:27 AM
Quote Reply
Re: [Matthias70] JSON In reply to
Matthias,
I can't remember why, but I decided to use another autosuggest method, using JQuery and JSON.
John
Quote Reply
Re: [gotze] JSON In reply to
  


i wonder if GT::Json could produce output to co-work with
datatables (datatables.net)

i would be interesting in have a sample in perl.
there are quite long time of non updating in links. but i am still in the hope that i could
see some advices from users here as it is the only why i could obtain the support for links now.


Thanks.
Quote Reply
Re: [courierb] JSON In reply to
Hi,

What/where are you trying to use datatables? I've seen a couple of GLinks sites using them on the search pages (for when you want to sort results etc).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] JSON In reply to
Andy


yes. i want to use datatables to arrange the search result
make it a nice sortable table . datatables has a lot of interesting
function.

i just want to have a smple . so i could follow it . i have tried a couple of time in vain.

Thanks
Quote Reply
Re: [courierb] JSON In reply to
Hi,

I need a bit more information that that :) I've seen it so that the user can sort the results showing on 1 page... but not with "span pages" (as that data would need to be requested via AJAX, which is where it gets more complex)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [gotze] JSON In reply to
   

Dear Andy

actually it can do a bit more than sorting in client side.
it will also reduce the server load and save a lot of bandwidth

some grid has zebra stripe, totalality counting , single or mutiple line select
,Pagination ,sorting, fixed table head, cell data modification....


here is the perl sample code from the offcical site.
http://www.datatables.net/...rver-side/perl_mysql


it's output looks like this

blah blah

Code:

my %sOutput = (
sEcho => int($q->param('sEcho')),
iTotalRecords => int($iTotal),
iTotalDisplayRecords => int($iFilteredTotal),
aaData => \@aaData,
);
return $self->to_json(\%sOutput);
} # /table_data



so basiclly i need to have sEcho iTotalRecords iTotalDisplayRecords aaData ready.

so i mimic its jason pattern to use following simple scripts to produce a jason response from perl.

Code:

#!c:/perl/bin/perl.exe
## modules json test,
use CGI;
use strict;
use warnings;

# read the CGI params
my $cgi = CGI->new;
my $sEcho = $cgi->param("sEcho");
my $username = "myname";
# create a JSON string according to the database result

$sEcho = int( $sEcho );
my $json =qq{{ "sEcho": $sEcho, "iTotalRecords": 5, "iTotalDisplayRecords": 5, "aaData": [ ["Trident","Internet Explorer 4.0","Win 95+","4","X"],["Trident","Internet Explorer 5.0","Win 95+","5","C"],["Trident","Internet Explorer 5.5","Win 95+","5.5","A"],["Trident","Internet Explorer 6","Win 98+","6","A"],["Trident","Internet Explorer 7","Win XP SP2+","7","A"]] }};
#"sEcho": 0,
#my $json = qq{{"error" : "username or password is wrong"}};
# print $cgi->header(-type => "application/json", -charset => "utf-8");
print $cgi->header( 'application/json' );
print $json;



but it seems something wrong. i can get table rendered. but it seems something wrong with it. i cant get do sorting or any other embeded datatables function.


Maybe i missed some facts , the author says
Quote:
The Perl script requires an extra parameter to be sent to it (rm), for which we can use fnServerData:
....blah
"sAjaxSource": "/cgi-bin/test/datatables/run.cgi",
"fnServerData": function ( sSource, aoData, fnCallback ) {
/* Add some extra data to the sender */
....blah




i am losted. because i do not understand its real meaning.


That is all i have . but it is just a unsucessful testing .

Last edited by:

courierb: Nov 20, 2011, 5:55 PM
Quote Reply
Re: [Andy] JSON In reply to
there is one disccustion cover that.
but it seems there is no published result.
http://www.datatables.net/...mystery-variable./p1
Quote Reply
Re: [courierb] JSON In reply to
 i understand now.


i need to feed databable with real data fetch from mysql.becase it request for new data when every i do any action on sorting or next page.
Quote Reply
Re: [courierb] JSON In reply to
Hi,

Did you get it working then?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] JSON In reply to
Dear Andy

Datatables a bit complicated for me at the first glance.
it has many way to load data to datatables. i was losted at first.


for server-side processing, i just realised that it 's sorting is done on server side script. when ever you want to sorting or change any table parameter. client side with contact server dor new data via ajax.


while if you load data by DOM or javascript array

[ "Trident", "Internet Explorer 4.0", "Win 95+", 4, "X" ],

you try to do sorting and pagination , all these work done on client side. unlike ajax based serverside processing (it will connect to server again and again).



i do had a sucessful partially test on the way of server side processing .

but i am interesting in load data by way of DOM or javascript array.
because my scenario is limited on one query on server then do a bit more of work on client side ( sorting , filtering , counting.......)

Cheers.

Last edited by:

courierb: Nov 22, 2011, 3:42 PM
Quote Reply
Re: [courierb] JSON In reply to
  

I still need to settle down how to run external js in the innerhtml.
as i plan to use ajax to load data to innerhtml, but if do a simple display. it is fine.
but if i want to have datatables to do any extra work, i will make datatables's scripts excute inside the innerhtml.

It seems it is more complicated for me .


cheers
Quote Reply
Re: [courierb] JSON In reply to
I'll try and have a look / play with it at some point to see if I can come up with an example script. Not sure when that will be though, as I'm still pretty bogged down

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!