Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
leeonsoft opened this issue Aug 8, 2022 · 5 comments
Closed

Comments

@leeonsoft
Copy link

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

@hydrobyte
Copy link
Owner

Could you provide a small example?

@leeonsoft
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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

@hydrobyte
Copy link
Owner

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants