Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem getting vector (array) of strings #44

Closed
amirmasoudabdol opened this issue Mar 4, 2015 · 7 comments
Closed

Problem getting vector (array) of strings #44

amirmasoudabdol opened this issue Mar 4, 2015 · 7 comments
Assignees

Comments

@amirmasoudabdol
Copy link
Contributor

Considering the JSON below,

{
     "names": ["Tim", "Tom"],
     "num": [1, 2]
}

I want to get a vector of string. I realized that I could only use auto a = json["name"] and then process it using for-range loop. However, if I want to explicitly define the return type then I will get an error. For instance, both vector<string> names = j["names"] and vector<string> names = j["names"].get<vector<string>>() will give me an error:

./json.hpp:829:24: error: no matching constructor for initialization of 'std::__1::vector<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > >'
                return T(m_value.array->begin(), m_value.array->end());

I can see that json library couldn't cast to vector<string>; however, it will work if I want to get num even explicitly like this: vector<float> f = j["num"].get<vector<float>>().

Now, I'm wondering if I am doing it wrong or it's not implemented?

Thanks,
Amir.

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2015

Dear Amir,

thanks for reporting! It sure looks like a bug. I shall have a look once I'm back from holidays.

All the best
Niels

@nlohmann nlohmann self-assigned this Mar 23, 2015
@nlohmann
Copy link
Owner

I could reproduce the bug.

@nlohmann
Copy link
Owner

Unfortunately, I cannot find a solution right now. I'll check again later.

@nlohmann
Copy link
Owner

nlohmann commented Apr 8, 2015

The last commit (1bdb6ac) fixed this issue. The following code compiles as expected:

#include <src/json.hpp>

using nlohmann::json;

int main()
{
    auto j = R"(
    {
         "names": ["Tim", "Tom"],
         "num": [1, 2]
    }
    )"_json;

    std::vector<std::string> names = j["names"];

    for (auto i : names)
    {
        std::cout << i << " ";
    }

    // prints: Tim Tom 
}

@nlohmann nlohmann closed this as completed Apr 8, 2015
@amirmasoudabdol
Copy link
Contributor Author

Thanks! :)

@chakpongchung
Copy link

#include <nlohmann/json.hpp>
#include <iostream>

using nlohmann::json;

int main() {
  auto j = R"(
    {
         "names": ["Tim", "Tom"],
         "num": [1, 2]
    }
    )"_json;

  std::vector<std::string> names;
  names = j["names"];

  for (auto i : names) {
    std::cout << i << " ";
  }

  // prints: Tim Tom
}

main.cpp:14:20: error: ambiguous overload for ‘operator=’ (operand types are ‘std::vector<std::__cxx11::basic_string >’ and ‘nlohmann::basic_json<>::value_type {aka nlohmann::basic_json<>}’)
names = j["names"];

Hi @nlohmann , separating the definition and declaration doesnt work again. Any solution for this? I need this.

@amirmasoudabdol
Copy link
Contributor Author

You modified the code slightly and that might affect your code. I think you should either use the names = j.at("names") or indicates the types in get again, names = j.get<std::vector<std:string>>().

GerHobbelt pushed a commit to GerHobbelt/nlohmann-json that referenced this issue May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants