I have a web page which has a JavaScript defining some data in a form:
var Categories = [ {name:"Cat1 ", caption:escape("Category 1")}, {name:"Cat2 ", caption:escape("Category 2")} ];
var DB = new Object(); DB[Categories[0].name] = [ {name:escape("Item1"), item-num:8} ];
etc...
When I try to parse it with cl-json (using decode-json-from-string), I got errors because it can't accept things like variable definitions. Can I use cl-json to create a CL data from the JS data I mentioned or I should use some other tool?
Thank you, Andrew
Hmm, what exactly are you parsing? Javascript? No in that case it can't be parsed. Not that json is not javascript, it is a subset of the javascript object notation.
What exaclty are you sending, in a string, to lisp?
Categories is an array, and DB is an object, so both have to be encoded to json. I would use some json client side library for that purpose. They are all around, in Dojo or Prototype or stand alone.
Best wishes, Henrik Hjelte
On Tue, Jun 10, 2008 at 12:46 AM, Andrei Stebakov lispercat@gmail.com wrote:
I have a web page which has a JavaScript defining some data in a form:
var Categories = [ {name:"Cat1 ", caption:escape("Category 1")}, {name:"Cat2 ", caption:escape("Category 2")}
];
var DB = new Object(); DB[Categories[0].name] = [ {name:escape("Item1"), item-num:8} ];
etc...
When I try to parse it with cl-json (using decode-json-from-string), I got errors because it can't accept things like variable definitions. Can I use cl-json to create a CL data from the JS data I mentioned or I should use some other tool?
Thank you, Andrew
cl-json-devel mailing list cl-json-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel
I was sending the string I mentioned starting with "var Categories = ....". Looks like I somehow need to translate the Categories and DB to some json tree, but I don't know how to do it. For output I need a lisp list/tree structure describing the same data something like '(:category "Cat1" (:name "Item1" :item-num 8) (:name "Item2" :iten-num 9)). Should I resort to flex/bison or I can just use the libraries available for common lisp?
Thank you, Andrew
On 6/10/08, Henrik Hjelte henrik@evahjelte.com wrote:
Hmm, what exactly are you parsing? Javascript? No in that case it can't be parsed. Not that json is not javascript, it is a subset of the javascript object notation.
What exaclty are you sending, in a string, to lisp?
Categories is an array, and DB is an object, so both have to be encoded to json. I would use some json client side library for that purpose. They are all around, in Dojo or Prototype or stand alone.
Best wishes, Henrik Hjelte
On Tue, Jun 10, 2008 at 12:46 AM, Andrei Stebakov lispercat@gmail.com wrote:
I have a web page which has a JavaScript defining some data in a form:
var Categories = [ {name:"Cat1 ", caption:escape("Category 1")}, {name:"Cat2 ", caption:escape("Category 2")}
]; var DB = new Object(); DB[Categories[0].name] = [ {name:escape("Item1"), item-num:8} ];
etc...
When I try to parse it with cl-json (using decode-json-from-string), I
got
errors because it can't accept things like variable definitions. Can I use cl-json to create a CL data from the JS data I mentioned or I should use some other tool?
Thank you, Andrew
cl-json-devel mailing list cl-json-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel
-- Henrik Hjelte henrik.hjelte@stix.to +46703993945 http://stix.to Lästmakarg 18-20 (IQube) Box 7438 S-103 91 Stockholm Sweden _______________________________________________ cl-json-devel mailing list cl-json-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel
On Tue, Jun 10, 2008 at 2:00 PM, Andrei Stebakov lispercat@gmail.com wrote:
I was sending the string I mentioned starting with "var Categories = ....".
ok, that was javascript source code, not json. When it is evaluated, it becomes a javascript array. That array can be encoded to Json, for that you need a javascript library to do the encoding. Look at www.json.org for a description of json and for available libraries, I believe that all major javascript frameworks have a json encoder.
For example if you have prototype it adds a method to all arrays:
var hello=[{a:2,b:3}]; console.log(hello.toJSON());
var hello=[{a:2,b:3}]; console.log(hello.toJSON());
[{"a": 2, "b": 3}] (output from Firebug, not the quotes around a and b when it is JSON)
Looks like I somehow need to translate the Categories and DB to some json tree, but I don't know how to do it.
I would look at the test cases for cl-json, they explain fairly well how to do encoding and decoding.
Should I resort to flex/bison or I can just use the libraries available for common lisp?
I don't know anything about flex or bison. But I say it is early to give up at this state, so far the problem has not been with cl-json.
Good luck, Henrik
I looked at the test cases for encoding/decoding. Looks like they all deal with json data structures starting with "[" and I couldn't find any method that would do anything similar to what you mentioned:
var hello=[{a:2,b:3}]; console.log(hello.toJSON());
[{"a": 2, "b": 3}] Did you suggest that I should find some library that would help me to evaluate my var hello=[{a:2,b:3}]; to [{"a": 2, "b": 3}] and then use cl-json to decode it to alist?
Andrew
On Tue, Jun 10, 2008 at 10:04 AM, Henrik Hjelte henrik@evahjelte.com wrote:
On Tue, Jun 10, 2008 at 2:00 PM, Andrei Stebakov lispercat@gmail.com wrote:
I was sending the string I mentioned starting with "var Categories =
....".
ok, that was javascript source code, not json. When it is evaluated, it becomes a javascript array. That array can be encoded to Json, for that you need a javascript library to do the encoding. Look at www.json.org for a description of json and for available libraries, I believe that all major javascript frameworks have a json encoder.
For example if you have prototype it adds a method to all arrays:
var hello=[{a:2,b:3}]; console.log(hello.toJSON());
var hello=[{a:2,b:3}]; console.log(hello.toJSON());
[{"a": 2, "b": 3}] (output from Firebug, not the quotes around a and b when it is JSON)
Looks like I somehow need to translate the Categories and DB to some json tree, but I don't know how to do it.
I would look at the test cases for cl-json, they explain fairly well how to do encoding and decoding.
Should I resort to flex/bison or I can just use the libraries available for common lisp?
I don't know anything about flex or bison. But I say it is early to give up at this state, so far the problem has not been with cl-json.
Good luck, Henrik