json: "type must be string, but is " std::string(j.type_name()

  • What is the issue you have? JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Create a JSON file with multiple objects and attempt to iterate with ` assert(baddie.is_array());

for (auto& json : jsonStream)
{
      name = json["Name"].get<std::string>();
   }`
  • What is the expected behavior? The file is properly read and parsed.

  • And what is the actual behavior instead? it gives the error above and an exception Unhandled exception at 0x00007FFF278EB098 in rpg.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x000000268ECFF168. occurred

Visual Studio 2017

  • Did you use a released version of the library or the version from the develop branch? From release.

Also this is for an entirely different project the solution for the question I posted worked perfectly.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (11 by maintainers)

Most upvoted comments

Ah, I see, the baddie is a json object that is defined in the class definition in another file. Looks like your problem is that you’re accessing "Attack" and "HP" without checking that they exist, and they don’t exist for the second object. You either need to check that they exist, or use the value() function that takes a default value.

Got it figured out, had to change the design of the parsing function it was indeed the while loop that was messing it all up thanks for the help!