使用 jsoncpp 从 json 迭代一个数组

Iterate an array from json using jsoncpp

我有以下 json:

{
    "laureates": [{
        "id": "1",
        "firstname": "Wilhelm Conrad",
        "surname": "Röntgen",
        "born": "1845-03-27",
        "died": "1923-02-10",
        "bornCountry": "Prussia (now Germany)",
        "bornCountryCode": "DE",
        "bornCity": "Lennep (now Remscheid)",
        "diedCountry": "Germany",
        "diedCountryCode": "DE",
        "diedCity": "Munich",
        "gender": "male",
        "prizes": [{
            "year": "1901",
            "category": "physics",
            "share": "1",
            "motivation": "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\""
        }]
    }]
}

我试过这个:

bool ok = reader.parse(txt, root, false);

    if(! ok)
    {
        std::cout << "failed parse\n";
    }

std::vector<std::string> keys = root.getMemberNames();

for (std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it)
    {
        if (root[*it].isString())
        {
            std::string value = root[*it].asString();
            std::cout << value << std::endl;
        }
        else if (root[*it].isInt())
        {
             int value = root[*it].asInt();
             std::cout << value << std::endl;
        }
        else if (root[*it].isArray()){
            // what to do here?
        }
}

代码工作正常,但问题是当我有一个像“奖品”这样的数组时。 如果不进行硬编码,我无法意识到如何迭代和显示值。

谁能帮我解决这个问题?

提前致谢。

I can't realize how to iterate and show the values without hardcoded it.

我认为问题是你没有很好地处理递归或 JSON 的键->值性质,因为当你有一个像“奖品”这样的数组时,你可以有一个嵌套 Json 对象,例如数组中的数组。

您可以使用递归来处理:

#include <jsoncpp/json/json.h>

#include <iostream>

void PrintJSONValue(const Json::Value &val) {
  if (val.isString()) {
    std::cout << val.asString();
  } else if (val.isBool()) {
    std::cout << val.asBool();
  } else if (val.isInt()) {
    std::cout << val.asInt();
  } else if (val.isUInt()) {
    std::cout << val.asUInt();
  } else if (val.isDouble()) {
    std::cout << val.asDouble();
  } else {
  }
}

void HandleJsonTree(const Json::Value &root, uint32_t depth = 0) {
  depth += 1;
  if (root.size() > 0) {
    std::cout << '\n';
    for (Json::Value::const_iterator itr = root.begin(); itr != root.end();
         itr++) {
      // print space to indicate depth
      for (int tab = 0; tab < depth; tab++) {
        std::cout << " ";
      }
      std::cout << "key: ";
      PrintJSONValue(itr.key());
      std::cout << " ";
      HandleJsonTree(*itr, depth);
    }
  } else {
    std::cout << ", value: ";
    PrintJSONValue(root);
    std::cout << "\n";
  }
}

int main(int argc, char **argv) {
  std::string json = R"###(
        {
    "laureates": [
        {
            "id": "1",
            "firstname": "Wilhelm Conrad",
            "surname": "Röntgen",
            "born": "1845-03-27",
            "died": "1923-02-10",
            "bornCountry": "Prussia (now Germany)",
            "bornCountryCode": "DE",
            "bornCity": "Lennep (now Remscheid)",
            "diedCountry": "Germany",
            "diedCountryCode": "DE",
            "diedCity": "Munich",
            "gender": "male",
            "prizes": [
                {
                    "year": "1901",
                    "category": "physics",
                    "share": "1",
                    "motivation": "in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him"
                }
            ]
        }
    ]
}
    )###";
  Json::Value root;
  Json::Reader reader;
  bool ok = reader.parse(json, root, false);

  if (!ok) {
    std::cout << "failed parse\n";
  }

  HandleJsonTree(root);
}

输出如下所示:

 key: laureates 
  key: 0 -- the first item of an array
   key: born , value: 1845-03-27
   key: bornCity , value: Lennep (now Remscheid)
   key: bornCountry , value: Prussia (now Germany)
   key: bornCountryCode , value: DE
   key: died , value: 1923-02-10
   key: diedCity , value: Munich
   key: diedCountry , value: Germany
   key: diedCountryCode , value: DE
   key: firstname , value: Wilhelm Conrad
   key: gender , value: male
   key: id , value: 1
   key: prizes 
    key: 0 -- the first item of an array
     key: category , value: physics
     key: motivation , value: in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him
     key: share , value: 1
     key: year , value: 1901
   key: surname , value: Röntgen