JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
It is often used for transmitting data between a server and a web application, as an alternative to XML.
Here is an example of a JSON object:
SNIPPET
1{
2 "name": "John Doe",
3 "age": 30,
4 "city": "New York"
5}
In the example above, the JSON object has three key-value pairs. The keys are strings and the values can be strings, numbers, booleans, arrays, or other nested objects.
To parse and manipulate JSON data in C++, you can use a library like nlohmann/json. This library provides an easy-to-use API for working with JSON objects.
Here is an example of how to create and manipulate a JSON object using the nlohmann/json library in C++:
xxxxxxxxxx
36
}
using namespace std;
int main()
{
// Create a JSON object
nlohmann::json data;
// Add key-value pairs to the JSON object
data["name"] = "John Doe";
data["age"] = 30;
data["city"] = "New York";
// Convert the JSON object to string
string jsonString = data.dump();
// Print the JSON string
cout << "JSON String: " << jsonString << endl;
// Parse the JSON string
nlohmann::json parsedData = nlohmann::json::parse(jsonString);
// Access values from the parsed JSON
string name = parsedData["name"];
int age = parsedData["age"];
string city = parsedData["city"];
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment