LINEMOを契約してPerplexity Proを一年間のあいだ無料で使います。
Ask 1
仕事でjsonを使いたい場面が発生。
make a program to parse json file by C++
(no file)
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Open the JSON file
std::ifstream file("example.json");
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
// Parse the JSON
json data;
try {
file >> data;
} catch (json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return 1;
}
// Access and print some data
std::cout << "Name: " << data["name"] << std::endl;
std::cout << "Age: " << data["age"] << std::endl;
// Access array elements
std::cout << "Hobbies:" << std::endl;
for (const auto& hobby : data["hobbies"]) {
std::cout << "- " << hobby << std::endl;
}
return 0;
}
参考。
Ask 2
同じtagを含んでいる場合に対応したい。
make a program parse json file include same tag by C++
(no file)
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
void printPerson(const json& person) {
std::cout << "Name: " << person["name"] << std::endl;
std::cout << "Age: " << person["age"] << std::endl;
std::cout << "City: " << person["city"] << std::endl;
std::cout << "Hobbies:" << std::endl;
for (const auto& hobby : person["hobbies"]) {
std::cout << " - " << hobby << std::endl;
}
std::cout << std::endl;
}
int main() {
// Open the JSON file
std::ifstream file("people.json");
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
// Parse the JSON
json data;
try {
file >> data;
} catch (json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return 1;
}
// Access and print data for each person
for (const auto& person : data["people"]) {
printPerson(person);
}
// Access and print summary data
std::cout << "Total people: " << data["summary"]["total_people"] << std::endl;
std::cout << "Average age: " << data["summary"]["average_age"] << std::endl;
return 0;
}
jsonのsampleも出力してくれる。
(no file)
{
"people": [
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": ["reading", "swimming"]
},
{
"name": "Jane Smith",
"age": 25,
"city": "Los Angeles",
"hobbies": ["painting", "yoga"]
},
{
"name": "Bob Johnson",
"age": 35,
"city": "Chicago",
"hobbies": ["coding", "gaming"]
}
],
"summary": {
"total_people": 3,
"average_age": 30
}
}
Ask 3
int型の値を取得したい。
make a program parse json file include same tag by C++, and get a integer value
(no file)
#include <iostream>
#include <fstream>
#include <vector>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Open the JSON file
std::ifstream file("data.json");
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
// Parse the JSON
json data;
try {
file >> data;
} catch (json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return 1;
}
// Vector to store the extracted integer values
std::vector<int> values;
// Extract integer values from the "items" array
for (const auto& item : data["items"]) {
if (item.contains("value") && item["value"].is_number_integer()) {
values.push_back(item["value"].get<int>());
}
}
// Print the extracted values
std::cout << "Extracted integer values:" << std::endl;
for (const auto& value : values) {
std::cout << value << std::endl;
}
// Calculate and print the sum of the values
int sum = 0;
for (const auto& value : values) {
sum += value;
}
std::cout << "Sum of values: " << sum << std::endl;
return 0;
}
Ask 4
float型の配列の値を取得したい。
make a program parse json file include same tag by C++, and get array of float values
(no file)
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Open the JSON file
std::ifstream file("data.json");
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return 1;
}
// Parse the JSON
json data;
try {
file >> data;
} catch (json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return 1;
}
// Vector to store the extracted float arrays
std::vector<std::vector<float>> allValues;
// Extract float arrays from the "items" array
for (const auto& item : data["items"]) {
if (item.contains("values") && item["values"].is_array()) {
std::vector<float> floatArray;
for (const auto& value : item["values"]) {
if (value.is_number()) {
floatArray.push_back(value.get<float>());
}
}
if (!floatArray.empty()) {
allValues.push_back(floatArray);
}
}
}
// Print the extracted float arrays
std::cout << "Extracted float arrays:" << std::endl;
for (size_t i = 0; i < allValues.size(); ++i) {
std::cout << "Item " << i + 1 << ": ";
for (const auto& value : allValues[i]) {
std::cout << std::fixed << std::setprecision(2) << value << " ";
}
std::cout << std::endl;
}
// Calculate and print the average of each array
std::cout << "\nAverages:" << std::endl;
for (size_t i = 0; i < allValues.size(); ++i) {
float sum = 0;
for (const auto& value : allValues[i]) {
sum += value;
}
float average = sum / allValues[i].size();
std::cout << "Item " << i + 1 << " average: " << std::fixed << std::setprecision(2) << average << std::endl;
}
return 0;
}
まとめ
素晴らしい。
広告
IT開発関連書とビジネス書が豊富な翔泳社の通販『SEshop』
さくらのレンタルサーバ

ムームードメイン

Oisix(おいしっくす)

らでぃっしゅぼーや

珈琲きゃろっと

エプソムソルト
