简体   繁体   English

从 nlohmann::json 访问元素?

[英]Access element from nlohmann::json?

I want to access element from a json which is the response from one query.我想访问 json 中的元素,这是一个查询的响应。

The json structure is: json结构为:

  json =   { "result": {
            "12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq": [
                20964,
                347474,
                347475
            ],
            "12ashmTiFStQ8RGUpi1BTCinJakVyDKWjRL6SWhnbxbT": [
                1992,
                1993,
                109096  
            ],
    }}

I want to get the 1st element(result[0]) key from result object ie 12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq in some variable a and the corresponding array ie [20964, 347474,347475 ] in some varible b.我想从结果12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq中获取第一个元素(结果[0])键,即某些变量 a 中的 12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq 和相应的数组,即某些变量 b 中的 [20964, 347474,347475 ]。

The problem I am having is that 1st element key value in this case "12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq" always changes for every query!我遇到的问题是在这种情况下"12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq"中的第一个元素键值总是针对每个查询更改!

Can someone show me the way how can I access it correctly?有人可以告诉我如何正确访问它吗?

json.begin() will give you an iterator pointing to the first element. json.begin()会给你一个指向第一个元素的迭代器。 Then you can access its' key and value using:然后您可以使用以下方法访问它的键和值:

auto key = json.begin().key();
auto value = json.begin().value();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM