nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.38k stars 6.67k forks source link

why a single-object json file appears in an array #4183

Closed Corgile closed 11 months ago

Corgile commented 11 months ago

Description

Unexpected '[' and ']', see pic:

image

Reproduction steps

minimal code to reproduce:


static nlohmann::json parseProfile(std::string profile) {
    std::ifstream file(profile);
    if (not file.is_open()) {
        INFO(RED("无法打开文件: "), profile);
        exit(EXIT_FAILURE);
    }
    return nlohmann::json::parse(file);
}
int main() {
    nlohmann::json profile{parseProfile("/home/brian/Projects/sqlite3-demo/profiles/demo.json")};

    std::cout << profile.dump(2);
}

Expected vs. actual results

expected:

{
  "one": 1,
  "two": "2",
  "tree": [
    1,2,3
  ]
}

actual:

[
  {
    "one": 1,
    "tree": [
      1,
      2,
      3
    ],
    "two": "2"
  }
]

Minimal code example

No response

Error messages

No response

Compiler and operating system

ubuntu 2204

Library version

v3.11.2

Validation

Corgile commented 11 months ago

if that's how it actually works, please explain why

nlohmann commented 11 months ago

See https://json.nlohmann.me/home/faq/#brace-initialization-yields-arrays