canellas
canellas

Reputation: 697

access array using 'nlohmann' JSON parser

I am trying to access an JSON array using 'nlohmann' library, as the example below shows:

#include <iostream>
#include <string>

#include "json.hpp"
using json = nlohmann::json;

int main() {
  {
    const std::string str(
        R"(
    {
       "result":{
          "lines":[
             {
                "i":1,
                "w":7,
             },
             {
                "i":1,
                "w":8,
             }
          ]
       },
       "success":true
    }
         )");

    json root(str);

    auto result = root.find("result");
    if (result != root.end()) {
      std::cout << *result << std::endl;
    } else {
      std::cout << "'result' not found\n";
    }
  }
}

Can anyone help and explain why the output is 'result' not found? According to the examples I read in https://github.com/nlohmann/json and other references I found, it should work.

Upvotes: 1

Views: 178

Answers (1)

canellas
canellas

Reputation: 697

I found the error.

I should be json root(json::parse(str)); and not json root(str);

Upvotes: 1

Related Questions