首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用nlohmann JSON解析json

使用nlohmann JSON解析json
EN

Stack Overflow用户
提问于 2018-02-16 19:22:34
回答 2查看 10.7K关注 0票数 0

我正在尝试使用nlohmann的json.hpp解析JSON结构。但我不会从字符串创建JSON结构。我已经尝试了所有的方法,但还是失败了。

我的要求是:

1)根据字符串创建JSON结构。

2)从中找到"statusCode“的值。

经过这么长时间的尝试,我真的开始怀疑nlohmann的json解析器是否支持嵌套JSON。

代码语言:javascript
运行
复制
#include "json.hpp"
using namespace std;

int main(){

    // giving error 1
    nlohmann::json strjson = nlohmann::json::parse({"statusResp":{"statusCode":"S001","message":"Registration Success","snStatus":"Active","warrantyStart":"00000000","warrantyEnd":"00000000","companyBPID":"0002210887","siteBPID":"0002210888","contractStart":"00000000","contractEnd":"00000000"}});

    // Giving error 2:
   auto j= "{
    "statusResp": {
        "statusCode": "S001",
        "message": "Registration Success",
        "snStatus": "Active",
        "warrantyStart": "20170601",
        "warrantyEnd": "20270601",
        "companyBPID": "0002210887",
        "siteBPID": "0002210888",
        "contractStart": "00000000",
        "contractEnd": "00000000"
    }
   }"_json;

   // I actually want to get the value of "statusCode" code from the JSOn structure. But no idea how to parse the nested value.
    return 1;

}

以下是两种初始化的错误:

代码语言:javascript
运行
复制
//ERROR 1:
test.cpp: In function 'int main()':
test.cpp:17:65: error: expected '}' before ':' token
     nlohmann::json strjson = nlohmann::json::parse({"statusResp":{"statusCode":"S001","message":"Registration Success","snStatus":"Active","warrantyStart":"00000000","warrantyEnd":"00000000","companyBPID":"0002210887","siteBPID":"0002210888","contractStart":"00000000","contractEnd":"00000000"}});

// ERROR 2:
hemanty@sLinux:/u/hemanty/workspaces/avac/cb-product/mgmt/framework/src/lib/libcurl_cpp$g++ test.cpp -std=gnu++11
test.cpp: In function 'int main()':
test.cpp:27:17: error: expected '}' before ':' token
     "statusResp": {
EN

回答 2

Stack Overflow用户

发布于 2018-02-19 19:36:21

因为"是字符串文字的开始和结束字符,所以在字符串中不能有"字符,而不能在其前面放置\

代码语言:javascript
运行
复制
std::string str = " "statusCode":"5001" "; //This does not work

std::string str = " \"statusCode\":\"5001\" "; //This will work

当您想要生成包含大量"的字符串时,一个更简单的替代方法是使用R"" string literal。然后你就可以这样写了。

代码语言:javascript
运行
复制
std::string str = R"("statusCode":"5001")";

如果我们现在将此转换为您的json示例,则解析字符串的正确方法将是以下方法之一。

代码语言:javascript
运行
复制
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
// and below the equivalent with raw string literal
auto j3 = json::parse(R"({"happy": true, "pi": 3.141 })");

//Here we use the `_json` suffix 
auto j2 = "
{
    \"happy\": true,
    \"pi\": 3.141
}"_json;
// Here we combine the R"" with _json suffix to do the same thing.
auto j2 = R"(
{
    "happy": true,
    "pi": 3.141
  }
)"_json;

取自readme的示例

票数 3
EN

Stack Overflow用户

发布于 2021-01-29 12:32:07

如果这就是你所需要的:

代码语言:javascript
运行
复制
std::string ss= R"(
{
    "test-data":
    [
        {
            "name": "tom",
            "age": 11
        },
        {
            "name": "jane",
            "age": 12
        }
    ]
}
)";

json myjson = json::parse(ss);
auto &students = myjson["test-data"];

for(auto &student : students) {
    cout << "name=" << student["name"].get<std::string>() << endl;
}

或者:

代码语言:javascript
运行
复制
json myjson = { {"name", "tom"}, {"age", 11} };
cout << "name=" << myjson["name"].get<std::string>() << endl;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48825709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档