#1 2015-02-04 01:13:21

toolater
Member
Registered: 2015-01-25
Posts: 7

decode Json string

I receive some Json data from webserver , but the contents were  encoded into Utf8,
"title":"\u5317\u4eac\u706b\u8f66\u7ad9\u552e\u7968\u5458\u8d2a\u6c61140\u4f59\u4e07\u9000\u7968\u6b3e \u5224\u521110\u5e74",
can I decode it?

Thank you for help

Offline

#2 2015-02-04 07:49:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,297
Website

Re: decode Json string

Yes.

Offline

#3 2015-02-07 00:10:40

toolater
Member
Registered: 2015-01-25
Posts: 7

Re: decode Json string

Thank you for reply, but the problem is I can not find the document how to decode by using mORMot .

Offline

#4 2015-02-07 07:58:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,297
Website

Re: decode Json string

The easiest is to use SynCommons.pas, then a TDocVariant:

var doc: variant;
begin
  doc := _JsonFast('{"title":"\u5317\u4eac\u706b\u8f66\u7ad9\u552e\u7968\u5458\u8d2a\u6c61140\u4f59\u4e07\u9000\u7968\u6b3e \u5224\u521110\u5e74"}');
  writeln(doc.title);
end;

Ensure you convert the JSON as expected into our RawUTF8 string, using e.g.

begin
  doc := _JsonFast(StringToUTF8(aStringContainingJson));
...

Offline

#5 2015-02-08 11:52:54

ruth
Member
Registered: 2015-02-08
Posts: 2

Re: decode Json string

If you're just trying to work out what the data is, I think https://json-csv.com decodes UTF8, then you can at least view the data there.

Offline

#6 2015-02-08 16:34:17

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,297
Website

Re: decode Json string

I know what the data is, it is just some Asiatic Unicode characters.
When I run the above code, doc.title contains '北京火车站售票员贪污140余万退票款 判刑10年'.
So conversion does just work.

Offline

Board footer

Powered by FluxBB