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

[json.exception.type_error.305] cannot use operator[] with a string argument with array #3931

Closed
2 tasks
wkingnet opened this issue Jan 29, 2023 · 2 comments
Closed
2 tasks

Comments

@wkingnet
Copy link

Description

This is my test: I create a json object, and I want save it to a file, and open it after. Unfortunately, the first step could not be completed. read json object directly is not possible.

#include <nlohmann/json.hpp>
#include <exception>
  json j = {
      {"player_id",
        {"addr", 0x011B90C4},
        {"offest", {}},
      },
      {"player_name",
        {"addr", 0x011B90B8},
        {"offest", {0x10,0x20,0x30}},
      }
  };
  cout << j.meta() << endl;
  cout << "j.type_name()=" << j.type_name() << endl;
  try {
    cout << j["player_id"] << endl;
  }
  catch (exception& e){
    cout << e.what() << endl;
  }

run the exe will be display a error: [json.exception.type_error.305] cannot use operator[] with a string argument with array

Reproduction steps

copy code and run

#include <nlohmann/json.hpp>
#include <exception>
  json j = {
      {"player_id",
        {"addr", 0x011B90C4},
        {"offest", {}},
      },
      {"player_name",
        {"addr", 0x011B90B8},
        {"offest", {0x10,0x20,0x30}},
      }
  };
  cout << j.meta() << endl;
  cout << "j.type_name()=" << j.type_name() << endl;
  try {

    cout << j["player_id"] << endl;
  }
  catch (exception& e){
    cout << e.what() << endl;
  }

Expected vs. actual results

Expected:
{"addr", 0x011B90C4},{"offest", {}},

actual results:

{"compiler":{"c++":"202002","family":"msvc","version":1934},"copyright":"(C) 2013-2022 Niels Lohmann","name":"JSON for Modern C++","platform":"win32","url":"https://github.com/nlohmann/json","version":{"major":3,"minor":11,"patch":2,"string":"3.11.2"}}
j.type_name()=array
[json.exception.type_error.305] cannot use operator[] with a string argument with array

Minimal code example

No response

Error messages

[json.exception.type_error.305] cannot use operator[] with a string argument with array

Compiler and operating system

"compiler":{"c++":"202002","family":"msvc","version":1934},"copyright":"(C) 2013-2022 Niels Lohmann","name":"JSON for Modern C++","platform":"win32","url":"https://github.com/nlohmann/json","version":{"major":3,"minor":11,"patch":2,"string":"3.11.2"

Library version

3.11.2

Validation

@nlohmann
Copy link
Owner

Apparently, you did not create an object, but an array. Please double check that you provide a list of pairs to the constructor, where the first element of each pair is a string.

@wkingnet
Copy link
Author

sorry, it was my mistake. I forgot a pair of {}. thank you very much for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants