get raw UTF8 data out of JSON.stringify?

6,169 views
Skip to first unread message

Egor Egorov

unread,
Jun 10, 2010, 8:38:30 AM6/10/10
to nodejs
Hi!

I need to get raw UTF-8 data out of JSON.stringify, without the \uXXXX
escaped sequences. How do I do that?

var sys = require('sys');
var struct = ['вася'];
var json_string = JSON.stringify(struct);
sys.debug(json_string);

It returns "DEBUG: ["\u0432\u0430\u0441\u044f"]" and I want "DEBUG:
["вася"]".

Akzhan Abdulin

unread,
Jun 10, 2010, 8:58:01 AM6/10/10
to nod...@googlegroups.com
You can do this after stringification using string 

json_string.replace(/\\u\d\d\d\d/g, function(a)
{
  // here convert sequence to correspond character.
});

2010/6/10 Egor Egorov <egor....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Jeff Lewis

unread,
Jun 10, 2010, 4:20:05 PM6/10/10
to nodejs
You could do something like:

...
sys.debug(unescape(json_string.replace(/\\u/g, '%u'));

Jeff

On Jun 10, 5:58 am, Akzhan Abdulin <akzhan.abdu...@gmail.com> wrote:
> You can do this after stringification using string
>
> json_string.replace(/\\u\d\d\d\d/g, function(a)
> {
>   // here convert sequence to correspond character.
>
> });
>
> 2010/6/10 Egor Egorov <egor.ego...@gmail.com>
>
>
>
> > Hi!
>
> > I need to get raw UTF-8 data out of JSON.stringify, without the \uXXXX
> > escaped sequences. How do I do that?
>
> > var sys = require('sys');
> > var struct = ['вася'];
> > var json_string = JSON.stringify(struct);
> > sys.debug(json_string);
>
> > It returns "DEBUG: ["\u0432\u0430\u0441\u044f"]" and I want "DEBUG:
> > ["вася"]".
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .

mario

unread,
Jun 11, 2010, 2:41:42 AM6/11/10
to nodejs
Reply all
Reply to author
Forward
0 new messages