Json示例

这里写目录标题

  • Cjson
  • [C++ JSon](#C++ JSon)
  • [QT json](#QT json)

Cjson

复制代码
#include  <stdio.h>
#include <stdlib.h>
#include "cjson.h"
// 向JSON对象中添加数据   
cJSON_AddStringToObject(root, "name", "John");
cJSON_AddNumberToObject(root, "age", 30);
cJSON_AddStringToObject(root, "city", "New York");

// 将JSON对象序列化为JSON字符串
char *jsonString = cJSON_Print(root);

if (jsonString) {
    printf("JSON String: %s\n", jsonString);

    // 解析JSON字符串为JSON对象
    cJSON *parsedRoot = cJSON_Parse(jsonString);

    if (parsedRoot) {
        cJSON *name = cJSON_GetObjectItem(parsedRoot, "name");
        cJSON *age = cJSON_GetObjectItem(parsedRoot, "age");
        cJSON *city = cJSON_GetObjectItem(parsedRoot, "city");

        if (name && age && city) {
            printf("Parsed JSON Data:\n");
            printf("Name: %s\n", name->valuestring);
            printf("Age: %d\n", age->valueint);
            printf("City: %s\n", city->valuestring);
        }

        // 释放解析后的JSON对象
        cJSON_Delete(parsedRoot);
    } else {
        printf("JSON parsing failed.\n");
    }

    // 释放JSON字符串
    free(jsonString);
} else {
    printf("JSON serialization failed.\n");
}

// 释放JSON对象
cJSON_Delete(root);

return 0;

}

`

cpp 复制代码
//嵌套对象使用
#include <stdio.h>
#include <stdlib.h>
#include <cJSON.h>

int main() {
    // 创建根对象
    cJSON *root = cJSON_CreateObject();

    // 创建嵌套对象1
    cJSON *nestedObject1 = cJSON_CreateObject();
    cJSON_AddStringToObject(nestedObject1, "name", "John");
    cJSON_AddNumberToObject(nestedObject1, "age", 30);

    // 添加嵌套对象1到根对象
    cJSON_AddItemToObject(root, "person1", nestedObject1);

    // 创建嵌套对象2
    cJSON *nestedObject2 = cJSON_CreateObject();
    cJSON_AddStringToObject(nestedObject2, "name", "Alice");
    cJSON_AddNumberToObject(nestedObject2, "age", 25);

    // 添加嵌套对象2到根对象
    cJSON_AddItemToObject(root, "person2", nestedObject2);

    // 将根对象转换为 JSON 字符串
    char *jsonString = cJSON_Print(root);

    // 输出 JSON 字符串
    printf("%s\n", jsonString);

    // 释放内存
    cJSON_Delete(root);
    free(jsonString);

    return 0;
}

C++ JSon

cpp 复制代码
#include <iostream>
#include "json.hpp"
#include <windows.h>
using namespace std;
namespace ns {
	// a simple struct to model a person
	struct person {
		std::string name;
		std::string address;
		int age;
	};
}

using json = nlohmann::json;
int main()
{
	//HWND hWnd = GetForegroundWindow(); // 获取当前窗口句柄
	//ShowWindow(hWnd, SW_HIDE); // 隐藏窗口

	ns::person p = { "Ned Flanders", "744 Evergreen Terrace", 60 };

	json j;
	j["name"] = p.name;
	j["address"] = p.address;
	j["age"] = p.age;

	cout << j << endl;

	while (1);
	return 0;
}

//json.cpp链接
链接:https://pan.baidu.com/s/1Q0TJAA4ifZ7UKCm_tULLLw 
提取码:c77q 
--来自百度网盘超级会员V2的分享

可以参考以下两个链接

https://github.com/nlohmann/json

http://nlohmann.github.io/json/api/basic_json/

QT json

cpp 复制代码
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

int main() {
    // 创建一个包含多个对象的 JSON 对象
    QJsonObject mainObject;

    // 创建子对象1
    QJsonObject person1;
    person1["name"] = "John";
    person1["age"] = 30;
    person1["city"] = "New York";

    // 创建子对象2
    QJsonObject person2;
    person2["name"] = "Alice";
    person2["age"] = 25;
    person2["city"] = "London";

    // 将子对象添加到主对象中
    mainObject["person1"] = person1;
    mainObject["person2"] = person2;

    // 创建 JSON 文档并将 JSON 对象添加到文档中
    QJsonDocument jsonDocument(mainObject);

    // 将 JSON 文档转换为字符串输出
    qDebug() << "JSON Data:\n" << jsonDocument.toJson();

    return 0;
}
cpp 复制代码
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

int main() {
    // JSON 数据字符串
    QString jsonData = R"(
        {
            "person1": {
                "name": "John",
                "age": 30,
                "city": "New York"
            },
            "person2": {
                "name": "Alice",
                "age": 25,
                "city": "London"
            }
        }
    )";

    // 将 JSON 数据字符串转换为 JSON 文档
    QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData.toUtf8());

    // 获取 JSON 文档的根对象
    QJsonObject mainObject = jsonDocument.object();

    // 从根对象中获取子对象
    QJsonObject person1 = mainObject["person1"].toObject();
    QJsonObject person2 = mainObject["person2"].toObject();

    // 从子对象中获取值
    QString name1 = person1["name"].toString();
    int age1 = person1["age"].toInt();
    QString city1 = person1["city"].toString();

    QString name2 = person2["name"].toString();
    int age2 = person2["age"].toInt();
    QString city2 = person2["city"].toString();

    // 输出解析得到的值
    qDebug() << "Person 1: " << name1 << ", " << age1 << ", " << city1;
    qDebug() << "Person 2: " << name2 << ", " << age2 << ", " << city2;

    return 0;
}
相关推荐
哈哈~haha4 小时前
ui5_Walkthrough_Step 7:JSON Model
json·mvc·module·ui5
随风一样自由6 小时前
React内逐行解释这个 package.json 文件,最近搞了个工厂AI生产平台,顺便来学习一下
学习·react.js·json·package
wtsolutions7 小时前
Excel to JSON by WTSolutions 4.0.0 版本更新公告
json·excel·wps·插件·转换·加载项·wtsolutions
wtsolutions7 小时前
Excel to JSON by WTSolutions 4.0.0 Update Announcement
json·excel·wps·addin·wtsolutions·conversion
最笨的羊羊18 小时前
Flink CDC系列之:Kafka 变更日志 JSON 格式工厂类 ChangeLogJsonFormatFactory
json·flink cdc系列·changelog·kafka 变更日志·json 格式工厂类·formatfactory
于是我说1 天前
Python Requests Session Cookies 与 JSON 文件的存取
python·json·dubbo
YAY_tyy1 天前
详解 3D Tiles 核心入口文件:tileset.json 结构与实战解析
3d·json·3dtiles
2***d8851 天前
SpringCloud系列教程:微服务的未来 (五)枚举处理器、JSON处理器、分页插件实现
spring cloud·微服务·json
k***92161 天前
深入了解 MySQL 中的 JSON_CONTAINS
数据库·mysql·json
map_3d_vis1 天前
JSAPIThree 数据源系统学习笔记:让数据在地图上可视化
json·学习笔记·csv·geojson·datasource·数据源·初学者·mapvthree·jsapithree