1

I have a very quick question...

I'm using nlohmann's json library.

My issue is that when I go to print a element, the program stops responding.

My json file

{
  "Consoleprinting": false,
  "Input" : [{"Code" : [{"Name": "EC", "Keybind": "VK_NUMPAD1"}] }]
}

What I have tried.

nlohmann::json jsonData = nlohmann::json::parse(i_Read);

std::cout << jsonData << std::endl;

for (auto& array : jsonData["Input"]) {
    std::cout << array["Code"] << std::endl;
}

^ This works but it prints out

[{"Name": "EC", "Keybind": "VK_NUMPAD1"}]

How can I get this it print out just the name?

4
  • array["Code"][0]["Name"]? Commented Oct 22, 2020 at 2:48
  • That worked! Thank you so much!!! Commented Oct 22, 2020 at 5:00
  • Good, but please don't edit the question to show the answer. Instead, post an answer. Commented Oct 22, 2020 at 5:17
  • Sorry! I'm new to using stack overflow. Commented Oct 22, 2020 at 7:28

1 Answer 1

1

array["Code"] is an array containing a single collection of key-value pairs, so you need to write:

std::cout << array["Code"][0]["Name"] << std::endl;
Sign up to request clarification or add additional context in comments.

2 Comments

I get it now! Due to the array not being set back to 0 it still thinks its at the other value correct?
No, you're just not printing the right thing. You need to access the elements you want, that's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.