Re: converting JSON to string



On Jan 11, 7:21 pm, Adonis Vargas <adon...@xxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Gowri wrote:
Hello,

I actually have two questions:
1. Are there any libraries which convert XML to JSON?
2. I am currently doing the above using the DOM parser and creating a
JSON array

<snippet>
for node in doc.getElementsByTagName("book"):
isbn = node.getAttribute("isbn")
titleNode = (node.getElementsByTagName("title")
[0]).childNodes[0]
title = titleNode.data
primarykeys.append({'isbn': isbn, 'title': title})
return primarykeys

I want to send primarykeys as a response to my client. i use
mod_python and apache. The problem is, I have not been able to figure
out how to convert my JSON output to a string.

Could someone please help me?

Thanks in advance

do:

return str(primarykeys)

Also there are Python modules for just this. Here is the very first link
from Google:

http://pypi.python.org/pypi/python-json

I have used this one personally and have been very satisfied with it.
There is another one (CJSON?) which is similar, but written in C, for
when performance may be an issue.

Hope this helps.

Adonis

Actually, I have one other problem after all this. I see that if I try
to construct JSON output as above, it is of the form
[{'isbn': u'1-56592-724-9', 'title': u'The Cathedral & the Bazaar'},
{'isbn': u'1-56592-051-1', 'title': u'Making TeX Work'}]
The extra 'u' seems to causing syntax error in JavaScript which is not
able to parse this response string. Any idea how I can fix this?
.



Relevant Pages

  • Re: XML<-> JSON conversion. What do you think?
    ... JSON limitations... ... JSON is not XML. ... Converting one to the other literally will ... var attrs = node.attributes; ...
    (comp.lang.javascript)
  • Re: More bollocks from biggots
    ... The paper don't say that the best workaround is to use a XML ... A server can mount a defense against JavaScript Hijacking by responding ... on adding support for JSON in future versions. ...
    (comp.os.vms)
  • Re: XML<-> JSON conversion. What do you think?
    ... of a conversion from XML to JSON doesn't work well. ... to use JSON to transmit simples data and XML for structured data. ... XML only has raw text and elements nodes. ...
    (comp.lang.javascript)
  • Re: XML<-> JSON conversion. What do you think?
    ... then most of implementations of conversion from XML to JSON are naïve! ...
    (comp.lang.javascript)
  • Re: New to JSON
    ... is writing an XML to JSON converter. ... and provide JSON to newer applications. ... This was a snippet of an auto-generated output from this converter. ...
    (microsoft.public.scripting.jscript)