欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

c+js解析json字符串數組

老白1年前9瀏覽0評論

C++和JavaScript都有自己的解析JSON字符串的方式,使用C++和JavaScript相結合,我們可以很方便地解析JSON字符串數組。

// C++代碼
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
std::string json_str = R"([
{
"name": "Alice",
"age": 20,
"address": {
"city": "New York",
"state": "NY",
"zip": "10001"
}
},
{
"name": "Bob",
"age": 25,
"address": {
"city": "Los Angeles",
"state": "CA",
"zip": "90001"
}
}
])";
json j = json::parse(json_str);
for (auto& element : j)
{
std::cout<< "Name: "<< element["name"].get<std::string>()<< std::endl;
std::cout<< "Age: "<< element["age"].get<int>()<< std::endl;
std::cout<< "Address: "<< element["address"]["city"].get<std::string>()<< ", "<< element["address"]["state"].get<std::string>()<< " "<< element["address"]["zip"].get<std::string>()<< std::endl;
}
return 0;
}

以上是使用nlohmann/json庫解析JSON字符串數組的C++代碼。

// JavaScript代碼
var json_str = `[
{
"name": "Alice",
"age": 20,
"address": {
"city": "New York",
"state": "NY",
"zip": "10001"
}
},
{
"name": "Bob",
"age": 25,
"address": {
"city": "Los Angeles",
"state": "CA",
"zip": "90001"
}
}
]`;
var obj = JSON.parse(json_str);
obj.forEach(function(element) {
console.log("Name: " + element.name);
console.log("Age: " + element.age);
console.log("Address: " + element.address.city + ", " + element.address.state + " " + element.address.zip);
});

以上是使用JSON.parse方法解析JSON字符串數組的JavaScript代碼。