hydrobyte / McJSON

A Delphi / Lazarus / C++Builder simple and small class for fast JSON parsing.
MIT License
58 stars 20 forks source link

When value is a json string, tostring does not handle escaping #4

Closed leeonsoft closed 1 year ago

leeonsoft commented 2 years ago

When value is a json string, tostring does not handle escaping

hydrobyte commented 2 years ago

Could you provide a small example?

leeonsoft commented 2 years ago

var json:TMcJsonItem; str:string; JsonObject:TJsonObject; begin try { TODO -oUser -cConsole Main : Insert code here } json:=TMcJsonItem.Create; JsonObject:=TJsonObject.Create; try json.add('topic').AsString:='test'; json.add('content').AsString:='{"name":"111"}'; Str:= json.ToString(false); writeln('mcJson:'+str); JsonObject.S['topic']:='test'; JsonObject.S['content']:='{"name":"111"}'; Str:= JsonObject.ToJSON(); writeln('JsonObject:'+str); Readln; finally json.Free; JsonObject.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

QQ图片20220808180252

hydrobyte commented 2 years ago

If you provide an escaped string representing a JSON object it will work. There is an example in Test11:

'{"k":"{\"key\":\"value\"}"}'

McJSON doesn't do the escapes for you.

However, the correct thing would be to throw an exception, since it is not being managed automatically.

I'll improve it to automatically manage this or throw an exception.

Thanks.

leeonsoft commented 2 years ago

I need mcjson to help me generate the correct json string so that other programs can parse it

hydrobyte commented 2 years ago

You can write a simple function in order to escape double quotes:

json.add('content').AsString:= MyEscapeDoubleQuotes('{"name":"111"}');