JSON trouble...

Hi,

We are testing out dhtmlx. We are trying to interface with our product API, for such we are using JSON. We made an initial test using the gridconnector generating XML. it worked fine.

Now trying to switch to JSON. We already have the functioning layout.

We tried loading the json with the ,“json” argument. but the griid is always empty.

I tried the following json:

{ "rows": [ { "id": "1421253", "data": [ "531717", "Nãoasasasasos", null, "2011-11-03 16:53:00", "Enasasasasa", "Dieasasos", "MaS" ] }, { "id": "1421272", "data": [ "532291", "888 Teste", "Cobraasasasida", "2011-11-16 17:28:00", "MED", "1111111", "aa" ] } ] }

Ultimately I’d want to use something more similar to a structured JSON, withour the “rows” aray, and the data aray within the rows array… would it be possible to accept something similar to what your JSONDataConnector outputs?

like so:

[ { "id": "1421253", "proto": "531717", "tit": null, "su": null, "Data": "2011-11-03 16:53:00", "st": "dfssdfdsf", "nm": "Didsfdsfpos", "es": "MS" }, { "id": "1421254", "proto": "531711", "tit": null, "su": null, "Data": "2011-11-03 17:34:00", "st": "sssssssssdas", "nm": "ssssss", "es": "ss" }

Thanks for all help. We have lots of places otuputting json already, having to change them all would be a pain. Having a way to parse this second model in the DHTMLX would be great.

So far we like the product very much. my co-worker has sent an email about this i think to the evaluation team, but if you can answer here to me its fine.

Now trying to switch to JSON.
How are you trying to switch to JSON? Are you using JSONDataConnector? Are you using latest version of dhtmlxConnectors? Do you attach dhtmlxgrid_json.js file to your page?

Ultimately I’d want to use something more similar to a structured JSON, withour the “rows” aray, and the data aray within the rows array… would it be possible to accept something similar to what your JSONDataConnector outputs?
Unfortunately its not possible

If you have included the dhtmlxgrid_json.js file and your json has “null’s”, dhtmlx has problems to evaluate it.
here is a fixed version of dhtmlxgrid_json.js

[code]dhtmlXGridObject.prototype._process_json_row = function(r, data) {
r._attrs = data;
for(var j = 0; j < r.childNodes.length; j++)
r.childNodes[j]._attrs = {};
if(data.userdata)
for(var a in data.userdata)
this.setUserData(r.idd, a, data.userdata[a])
for(var i = 0; i < data.data.length; i++)
if( typeof data.data[i] == “object”) {
if(data.data[i] == null) data.data[i] = “”;
r.childNodes[i]._attrs = data.data[i];

	if(data.data[i].type)
		r.childNodes[i]._cellType = data.data[i].type;
	data.data[i] = data.data[i].value
};
this._fillRow(r, (this._c_order ? this._swapColumns(data.data) : data.data));
return r

};
dhtmlXGridObject.prototype._process_json = function(data) {
this._parsing = true;
if(data && data.xmlDoc)
eval(“data=” + data.xmlDoc.responseText + “;”);
else if( typeof data == “string”)
eval(“data=” + data + “;”)
var cr = parseInt(data.pos || 0);
var total = parseInt(data.total_count || 0);
if(total && !this.rowsBuffer[total - 1])
this.rowsBuffer[total - 1] = null;
if(this.isTreeGrid())
return this._process_tree_json(data);
for(var i = 0; i < data.rows.length; i++) {
if(this.rowsBuffer[i + cr])
continue;
var id = data.rows[i].id;
this.rowsBuffer[i + cr] = {
idd : id,
data : data.rows[i],
_parser : this._process_json_row,
_locator : this._get_json_data
};
this.rowsAr[id] = data[i]
};
this.render_dataset();
this._parsing = false
};
dhtmlXGridObject.prototype._process_tree_json = function(data, top, pid) {
this._parsing = true;
var main = false;
if(!top) {
this.render_row = this.render_row_tree;
main = true;
top = data;
pid = top.parent || 0;
if(pid == “0”)
pid = 0;
if(!this._h2)
this._h2 = new dhtmlxHierarchy();
if(this._fake)
this._fake._h2 = this._h2
};
if(top.rows)
for(var i = 0; i < top.rows.length; i++) {
var id = top.rows[i].id;
var row = this._h2.add(id, pid);
row.buff = {
idd : id,
data : top.rows[i],
_parser : this._process_json_row,
_locator : this._get_json_data
};
if(top.rows[i].open)
row.state = “minus”;
this.rowsAr[id] = row.buff;
this._process_tree_json(top.rows[i], top.rows[i], id)
}
;
if(main) {
if(pid != 0)
this._h2.change(pid, “state”, “minus”)
this._updateTGRState(this._h2.get[pid]);
this._h2_to_buff();
this.render_dataset();
if(this._slowParse === false) {
this.forEachRow(function(id) {
this.render_row_tree(0, id)
})
};
this._parsing = false
}
};[/code]

I anded up using a nonstandard JSON view on my API… My idea was to initially just output the result array, like with the other functions, but to build the dhtmlx grid I realized it wouldnt be possible… It is strange because JSONDDataConnector does output in this format… anyways, I will try with my “adapted method” (it isnt very elegant but it is working so far)…

Hi guys,
stoeffel, good job, thanks.

RVN, you may use dhtmlxDataStore to load standart JSON generated by JSONDataConnector.
JSONDataConnector was designed for datastore, not for grid, so you may load data into datastore and then sync datastore with grid.
DataStore docs:
docs.dhtmlx.com/doku.php?id=dhtmlxdatastore:toc
Usage sample:

	var data = new dhtmlXDataStore({
		url:"../common/data/data.json",
		datatype:"json"
	});

	var mygrid = new dhtmlXGridObject('gridbox');
	mygrid.setSkin("dhx_skyblue")
	mygrid.setHeader("Count,Name");
	mygrid.setColumnIds("Maintainer,Version")
	mygrid.init();
	mygrid.sync(data);