【问题标题】:C++ XE2 - How to read Nested JSON?C++ XE2 - 如何读取嵌套 JSON?
【发布时间】:2012-07-09 02:26:01
【问题描述】:

我一直在寻找如何使用 C++ Builder XE2 TJSONObject 读取嵌套 JSON。

在Delphi中有几个例子,但是他们使用TJSONValue对象,但是在C++版本这个类是纯虚函数,不能被创建。

一些示例 JSON:

{
  "totalHits": 4170,
  "totalCount": 4170,
  "startIndex": 0,
  "adverts": [
    {
      "Id": "14380005",
      "companyInfo": {
        "companyName": "Clarion Hotel Sign",
        "orgNumber": "5564660107",
        "companyText": "hotell"
      },
      "address": {
        "streetName": "Street race 2",
        "postCode": "101 26",
        "postArea": "MY AREA",
        "postBox": "Box 310"
      },
      "homepage": "www.mypage.net"
    }
  ]
}

整个 JSON 存储在 JSON 对象中,相信我,它就在里面 :)

TJSONObject *JSON = new TJSONObject;

获取totalHits 和totalCount 的值没有问题,但是如何获取"companyName" 值?!?

谢谢

【问题讨论】:

    标签: json nested-sets c++builder-xe2


    【解决方案1】:

    我为将来遇到同样问题的其他人发布这个迟来的答案....

    TJSONArray* jArray = (TJSONArray* )JSON->Get("adverts")->JsonValue;
    TJSONObject* jCompanyInfo = (TJSONObject*)((TJSONObject*)jArray->Get(0))
           ->Get("companyInfo")->JsonValue);
    String companyName =  jCompanyInfo->Get("companyName")->JsonValue->Value());
    

    【讨论】:

    • 我创建了一个带有 Key=value StringList 层次结构的“文档”。以这种方式更容易搜索和阅读。我接受你的回答,因为我想这是正确的做法:)
    猜你喜欢
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多