当需要处理的json文件包含多个结构体对象如何处理?

① 当需要处理的json文件包含多个结构体对象,比如json文件:{ “weight_group1”: { “coreid”: 1, “weight”: 60.5 }, “weight_group2”: { “coreid”: 2, “weight”: 65.2 }, “weight_group3”: { “coreid”: 3, “weight”: 70.1 } }

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

typedef struct {
    int coreid;
    double weight;
} WeightGroup;

int main() {
    const char* json_data = "{\"weight_group1\":{\"coreid\":1,\"weight\":60.5},\"weight_group2\":{\"coreid\":2,\"weight\":65.2},\"weight_group3\":{\"coreid\":3,\"weight\":70.1}}";

    cJSON* root = cJSON_Parse(json_data);
    if (root == NULL) {
        printf("Failed to parse JSON\n");
        return 1;
    }

    WeightGroup weightGroups[3];

    cJSON* weightGroup1 = cJSON_GetObjectItem(root, "weight_group1");
    if (weightGroup1 != NULL) {
        cJSON* coreidItem = cJSON_GetObjectItem(weightGroup1, "coreid");
        cJSON* weightItem = cJSON_GetObjectItem(weightGroup1, "weight");
        if (coreidItem != NULL && weightItem != NULL) {
            weightGroups[0].coreid = coreidItem->valueint;
            weightGroups[0].weight = weightItem->valuedouble;
        }
    }

    cJSON* weightGroup2 = cJSON_GetObjectItem(root, "weight_group2");
    if (weightGroup2 != NULL) {
        cJSON* coreidItem = cJSON_GetObjectItem(weightGroup2, "coreid");
        cJSON* weightItem = cJSON_GetObjectItem(weightGroup2, "weight");
        if (coreidItem != NULL && weightItem != NULL) {
            weightGroups[1].coreid = coreidItem->valueint;
            weightGroups[1].weight = weightItem->valuedouble;
        }
    }

    cJSON* weightGroup3 = cJSON_GetObjectItem(root, "weight_group3");
    if (weightGroup3 != NULL) {
        cJSON* coreidItem = cJSON_GetObjectItem(weightGroup3, "coreid");
        cJSON* weightItem = cJSON_GetObjectItem(weightGroup3, "weight");
        if (coreidItem != NULL && weightItem != NULL) {
            weightGroups[2].coreid = coreidItem->valueint;
            weightGroups[2].weight = weightItem->valuedouble;
        }
    }

    // 打印解析结果
    for (int i = 0; i < 3; i++) {
        printf("Weight Group %d\n", i + 1);
        printf("Core ID: %d\n", weightGroups[i].coreid);
        printf("Weight: %.2f\n", weightGroups[i].weight);
        printf("\n");
    }

    cJSON_Delete(root);

    return 0;
}

这个示例代码将解析你提供的 JSON 数据,并将每个 weight_group 对象的 coreid 和 weight 字段存储在 WeightGroup 结构体数组中。然后,它会打印出每个 weight_group 对象的 coreid 和 weight 值。

请注意,这个示例代码中假设 JSON 数据中的 weight_group 对象的名称是固定的(weight_group1、weight_group2、weight_group3)。如果 JSON 数据中的 weight_group 对象的数量和名称是不确定的,你可能需要使用 cJSON 库提供的遍历函数来动态处理这些对象。

②如果需要处理的json文件包含多个键值对,每个键值对代表一个权重组。每个权重组都包含 core、neuron、synapse 和 weight 字段。比如:{ “fc0_0-weight”: { “core”: 0, “neuron”: [ “0-31” ], “synapse”: [ “0-195” ], “weight”: [ 1e-05, 0.0001, 1e-05, 1e-05, 1e-05, 0.0001, 1e-05, 1e-05 ] }, “fc0_1-weight”: { “core”: 1, “neuron”: [ “0-31” ], “synapse”: [ “0-195” ], “weight”: [ 1e-05, 0.0001, 1e-05, 1e-05, 1e-05, 0.0001 ] } }。

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

typedef struct {
    int core;
    char* neuron;
    char* synapse;
    double* weight;
    int weightCount;
} WeightGroup;

int main() {
    const char* json_data = "{\"fc0_0-weight\":{\"core\":0,\"neuron\":[\"0-31\"],\"synapse\":[\"0-195\"],\"weight\":[1e-05,0.0001,1e-05,1e-05,1e-05,0.0001,1e-05,1e-05]},\"fc0_1-weight\":{\"core\":1,\"neuron\":[\"0-31\"],\"synapse\":[\"0-195\"],\"weight\":[1e-05,0.0001,1e-05,1e-05,1e-05,0.0001]}}";

    cJSON* root = cJSON_Parse(json_data);
    if (root == NULL) {
        printf("Failed to parse JSON\n");
        return 1;
    }

    cJSON* weightGroup1 = cJSON_GetObjectItem(root, "fc0_0-weight");
    cJSON* weightGroup2 = cJSON_GetObjectItem(root, "fc0_1-weight");

    WeightGroup weightGroups[2];

    // 解析第一个权重组
    cJSON* coreItem1 = cJSON_GetObjectItem(weightGroup1, "core");
    cJSON* neuronItem1 = cJSON_GetObjectItem(weightGroup1, "neuron");
    cJSON* synapseItem1 = cJSON_GetObjectItem(weightGroup1, "synapse");
    cJSON* weightArray1 = cJSON_GetObjectItem(weightGroup1, "weight");

    if (coreItem1 != NULL && coreItem1->type == cJSON_Number &&
        neuronItem1 != NULL && neuronItem1->type == cJSON_Array &&
        synapseItem1 != NULL && synapseItem1->type == cJSON_Array &&
        weightArray1 != NULL && weightArray1->type == cJSON_Array) {

        weightGroups[0].core = coreItem1->valueint;
        weightGroups[0].neuron = cJSON_GetArrayItem(neuronItem1, 0)->valuestring;
        weightGroups[0].synapse = cJSON_GetArrayItem(synapseItem1, 0)->valuestring;

        int weightCount1 = cJSON_GetArraySize(weightArray1);
        weightGroups[0].weightCount = weightCount1;
        weightGroups[0].weight = (double*)malloc(weightCount1 * sizeof(double));

        for (int i = 0; i < weightCount1; i++) {
            cJSON* weightItem = cJSON_GetArrayItem(weightArray1, i);
            if (weightItem != NULL && weightItem->type == cJSON_Number) {
                weightGroups[0].weight[i] = weightItem->valuedouble;
            }
        }
    }

    // 解析第二个权重组
    cJSON* coreItem2 = cJSON_GetObjectItem(weightGroup2, "core");
    cJSON* neuronItem2 = cJSON_GetObjectItem(weightGroup2, "neuron");
    cJSON* synapseItem2 = cJSON_GetObjectItem(weightGroup2, "synapse");
    cJSON* weightArray2 = cJSON_GetObjectItem(weightGroup2, "weight");

    if (coreItem2 != NULL && coreItem2->type == cJSON_Number &&
        neuronItem2 != NULL && neuronItem2->type == cJSON_Array &&
        synapseItem2 != NULL && synapseItem2->type == cJSON_Array &&
        weightArray2 != NULL && weightArray2->type == cJSON_Array) {

        weightGroups[1].core = coreItem2->valueint;
        weightGroups[1].neuron = cJSON_GetArrayItem(neuronItem2, 0)->valuestring;
        weightGroups[1].synapse = cJSON_GetArrayItem(synapseItem2, 0)->valuestring;

        int weightCount2 = cJSON_GetArraySize(weightArray2);
        weightGroups[1].weightCount = weightCount2;
        weightGroups[1].weight = (double*)malloc(weightCount2 * sizeof(double));

        for (int i = 0; i < weightCount2; i++) {
            cJSON* weightItem = cJSON_GetArrayItem(weightArray2, i);
            if (weightItem != NULL && weightItem->type == cJSON_Number) {
                weightGroups[1].weight[i] = weightItem->valuedouble;
            }
        }
    }

    // 打印解析结果
    for (int i = 0; i < 2; i++) {
        printf("Weight Group %d\n", i + 1);
        printf("Core: %d\n", weightGroups[i].core);
        printf("Neuron: %s\n", weightGroups[i].neuron);
        printf("Synapse: %s\n", weightGroups[i].synapse);
        printf("Weight Count: %d\n", weightGroups[i].weightCount);
        printf("Weights: ");
        for (int j = 0; j < weightGroups[i].weightCount; j++) {
            printf("%.5f ", weightGroups[i].weight[j]);
        }
        printf("\n\n");
    }

    // 释放内存
    for (int i = 0; i < 2; i++) {
        free(weightGroups[i].weight);
    }
    cJSON_Delete(root);

    return 0;
}

这个示例代码将解析你提供的 JSON 数据,并将每个权重组的 core、neuron、synapse 和 weight 字段存储在 WeightGroup 结构体数组中。然后,它会打印出每个权重组的字段值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值