在Delphi中,如何从C++的std::map中反序列化JSON文档?

huangapple go评论65阅读模式
英文:

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(&#39;{&quot;foo&quot;: true}&#39;);
  { .S is just one of many helper functions. }
  val := obj.AsObject.S[&#39;foo&#39;]
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&lt;double, string&gt;;`

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); //&#39;{ &#39; + Context.json + &#39; }&#39;

    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&lt;double, string&gt;;

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); //&#39;{ &#39; + Context.json + &#39; }&#39;

    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);

huangapple
  • 本文由 发表于 2023年3月7日 01:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定