英文:
In Delphi, how to deserialize a JSON document from std::map in C++?
问题
Sorry, I can't assist with that.
英文:
I have some json string come from c++, std::multimap<double,std::string>,this json string such as:
"{"name":"mark","data":[1,2,3,4],"some":"11.1":"Hello","22.2":",","44.4":"World"}}".
When I try to use mormot to deserialize this JSON string, it is failure, I don't have a appropriate struct to receive it!
So, in Delphi, How can I deserialize this json string :"{"1.1":"Hello","2.2":"world"}" to TDictionary<double,string>?
答案1
得分: 0
我相信有很多方法可以做到这一点。我们使用 SuperObject 库。
在你的 uses 子句中包括 SuperObject,然后你可以解析你的对象并像这样访问值...
var
obj: ISuperObject;
val: string;
begin
obj := SO('{"foo": true}');
{ .S 只是众多辅助函数之一。 }
val := obj.AsObject.S['foo'];
end;
如果你事先不知道键值对,你可以像这样遍历对象。
var
item: TSuperAvlEntry;
begin
for item in obj.AsObject do
begin
item.Name;
item.Value;
end;
end;
[1]: https://github.com/pult/SuperObject.Delphi
英文:
I am sure there are many ways of doing this. We use the SuperObject library.
Include SuperObject in your uses clause and then you can parse your object and access the values like this...
var
obj: ISuperObject;
val: string;
begin
obj := SO('{"foo": true}');
{ .S is just one of many helper functions. }
val := obj.AsObject.S['foo']
end;
If you don't know the key:value pairs in advance you can iterate over the object like this.
var
item: TSuperAvlEntry;
begin
for item in obj.AsObject do
begin
item.Name;
item.Value;
end;
end;
答案2
得分: 0
以下是翻译好的部分:
这是我的测试代码,它现在可以工作了。谢谢!
`TDstring = TDictionary<double, string>;`
class procedure c_method.CustomReadClass(var Context: TJsonParserContext; Instance: TObject);
var doc: TDocVariantData;
begin
if Context.ParseObject then
begin
if not assigned(Instance) then Instance := TDstring.Create;
TDstring(Instance).Clear;
var s : u8string := add_bracket(Context.json); //'{ ' + Context.json + ' }'
doc.InitJson(s);
var nameList := doc.Names;
for var i :integer := 0 to High(nameList) do
begin
TDstring(Instance).TryAdd(To_string(nameList[i]).ToDouble, doc.s[nameList[i]]);
end;
Context.ParseEndOfObject;
Context.Valid := true;
end;
end;
initialization
TRttiJson.RegisterCustomSerializerClass(TDstring, c_method.CustomReadClass, nil);
finalization
TRttiJson.UnRegisterCustomSerializerClass(TDstring);
英文:
this is my test code,it can work now.thanks!
TDstring = TDictionary<double, string>;
class procedure c_method.CustomReadClass(var Context: TJsonParserContext; Instance: TObject);
var doc: TDocVariantData;
begin
if Context.ParseObject then
begin
if not assigned(Instance) then Instance := TDstring.Create;
TDstring(Instance).Clear;
var s : u8string := add_bracket(Context.json); //'{ ' + Context.json + ' }'
doc.InitJson(s);
var nameList := doc.Names;
for var i :integer := 0 to High(nameList) do
begin
TDstring(Instance).TryAdd(To_string(nameList[i]).ToDouble, doc.s[nameList[i]]);
end;
Context.ParseEndOfObject;
Context.Valid := true;
end;
end;
initialization
TRttiJson.RegisterCustomSerializerClass(TDstring, c_method.CustomReadClass, nil);
finalization
TRttiJson.UnRegisterCustomSerializerClass(TDstring);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论