[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

converting JSON to string

jrrtolkienfan@gmail.com

1/12/2008 12:13:00 AM

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
5 Answers

Adonis Vargas

1/12/2008 1:21:00 AM

0

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/p...

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

jrrtolkienfan@gmail.com

1/12/2008 1:54:00 AM

0

Hi Adonis,

Thanks so much. Appreciate it :)

jrrtolkienfan@gmail.com

1/12/2008 7:32:00 AM

0

On Jan 11, 7:21 pm, Adonis Vargas <adon...@REMOVETHISearthlink.net>
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/p...
>
> 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?

Jeroen Ruigrok van der Werven

1/12/2008 8:58:00 AM

0

-On [20080112 08:38], Gowri (gowricp@gmail.com) wrote:
>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?

JSON does not support Unicode in the sense of allowing raw Unicode codepoints.
Instead JSON uses a \uNNNN scheme to encode Unicode characters (a bit flawed
to limit it to four hexadecimal digits though, it leaves the whole CJK Unified
Ideographs Extension B out of scope.).

I use simplejson along with lxml/ElementTree for my JSON<>XML needs.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
ã?¤ã?§ã?«ã?¼ã?³ ã?©ã?¦ã??ã?­ã??ã?¯ ã?´ã?¡ã?³ ã??ã?« ã?¦ã?§ã?«ã?´ã?§ã?³
http://www.in-n... | http://www.ra...
Any road leads to the end of the world...

jrrtolkienfan@gmail.com

1/13/2008 11:44:00 PM

0

On Jan 12, 2:58 am, Jeroen Ruigrok van der Werven <asmo...@in-
nomine.org> wrote:
> -On [20080112 08:38], Gowri (gowr...@gmail.com) wrote:
>
> >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?
>
> JSON does not support Unicode in the sense of allowing raw Unicode codepoints.
> Instead JSON uses a \uNNNN scheme to encode Unicode characters (a bit flawed
> to limit it to four hexadecimal digits though, it leaves the whole CJK Unified
> Ideographs Extension B out of scope.).
>
> I use simplejson along with lxml/ElementTree for my JSON<>XML needs.
>
> --
> Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
> ????? ?????? ??? ?? ??????http://www.in-n...|http://www.ra...
> Any road leads to the end of the world...

Thanks Jeroen. That helped a lot :)