How do I convert JSON array of objects into gid format?

Your assistance with the following would be appreciated:-

I want to feed some data into a DHTMLX grid using JSON. In your tutorial docs.dhtmlx.com/doku.php?id=dhtm … _from_json you illustrate a format which looks like this:-

var js={
rows:[
{ id:1001,
data:[
“100”,
“A Time to Kill”,
“John Grisham”,
“12.99”,
“1”,
“05/01/1998”] },
{ id:1002,
data:[
“1000”,
“Blood and Smoke”,
“Stephen King”,
“0”,
“1”,
“01/01/2000”] }
]}
grid.parse(js,“json”);

I have experimented with this and it works very well.

My problem is, how do I get my data into that format in the first place?

My data originally comes from a “Redback object” (Rocket U2 Web Designer) although that is unimportant – I format the data into strings into a server C# List<> object (that too can be flexible). I have then experimented with serializing it like this, before copying it across to a Javascript function (using a hidden field):-

JavaScriptSerializer oSerializer = new JavaScriptSerializer();

    SerialString = oSerializer.Serialize(lstCSToJSON);

After Serialization it looks like this, which is of course what I would expect a JSON array of objects to look like:-
“[{“Col1”:“this”,“Col2”:“is”,“Col3”:“the”,“Col4”:“first”,“Col5”:“row”,“Col6”:“for test”},{“Col1”:“this”,“Col2”:“is”,“Col3”:“the”,“Col4”:“second”,“Col5”:“row”,“Col6”:“for test”},{“Col1”:“this”,“Col2”:“is”,“Col3”:“the”,“Col4”:“third”,“Col5”:“row”,“Col6”:“for test”}]”

However the grid won’t accept it using grid.parse(variablename, “json”).

How do I convert the second format to the first?

This is how I’ve tried to pass it to the grid:-
var mygridSerialized;
function doInitGridSerialized(SerializedNowInJava1){
mygridSerialized = new dhtmlXGridObject(‘mygrid_container’);
mygridSerialized.attachEvent(“onRowSelect”,doOnRowSelected);
mygridSerialized.setImagePath(“codebase/imgs/”);
mygridSerialized.setHeader(“Col1, Col2, Col3, Col4, Col5, Col6”);
mygridSerialized.setInitWidths(“100,100,100,100,100,100”);
mygridSerialized.setColAlign(“left,left,left,left,left,left”);
mygridSerialized.setSkin(“light”);
mygridSerialized.init();
mygridSerialized.setColSorting(“str,str,str,str,str,str”);
mygridSerialized.setColTypes(“ed,ed,ed,ed,ed,ed”);
mygridSerialized.parse(SerializedNowInJava1,“json”);
}

This is the error message I get:-

Microsoft JScript runtime error: ‘rows.length’ is null or not an object.

Thanks everyone

You can convert JSON string to object with following code:

var myObject = eval(’(’ + myJSONtext + ‘)’);

json.org/js.html

Thanks Olga but after eval it still will not parse into the grid. The forum will not allow me to attach a word doc. I would like to have shown you the value of the variable. In the immediate window it looks like this:-

{…}
[0]: {…}
[1]: {…}
[2]: {…}
length: 3

…which is different to the format in your tutorial, which worked, which was:-

var js={
rows:[
{ id:1001,
data:[
“100”,
“A Time to Kill”,
“John Grisham”,
“12.99”,
“1”,
“05/01/1998”] },
{ id:1002,
data:[
“1000”,
“Blood and Smoke”,
“Stephen King”,
“0”,
“1”,
“01/01/2000”] }
]}

Thanks

Roger

I know this is an old post, but I have this exact same issue, and was wondering if you ever happened to get it resolved. I am using eval to convert a string into a JSON object, just as Olga suggests, but I get the same error you do.

Thanks!

Try This one :slight_smile:

Hide Expand Copy Code

{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://your json URL”);

            //&tracking_number=" + txtttnumber.Text + "&courier=" + ddlcourier.SelectedValue

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;

                if (response.CharacterSet == null)
                    readStream = new StreamReader(receiveStream);
                else
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                //  List<string> list = new List<string>();

//Convert JSON object into String
string data = readStream.ReadToEnd();
//Convert JSON object into Dictionary
Dictionary<string, object> dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(data);

//Convert into variable