如何将数组转成json 数据格式

例如 0b 13 b3 95 48 9d 3e 63 9c c4 70 f4 50 85 55 5e 数据
start cmd[ 27 01 f7 02 10 0b 13 b3 95 48 9d 3e 63 9c c4 70 f4 50 85 55 5e 01 01 50 FB ]

set_potk json 数据10进制 VALUE 11,19,179,149,72,157,62,99,156,196,112,244,80,133,85,94

  • {“jsonrpc”:“2.0”,“type”:2,“id”:0,“method”:“method”,“message”:{“VALUE”:[11,19,179,149,72,157,62,99,156,196,112,244,80,133,85,94]},“N”:0,“query”:“”,“uri”:"f}
    {“id”:0,“jsonrpc”:“2.0”,“result”:{“SUCCESS”:0},“uri”:“factory_cmd/sys/key/potk”}
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
{
    cJSON *array = cJSON_CreateArray();
    if (add_item_to_object(object, name, array, &global_hooks, false))
    {
        return array;
    }

    cJSON_Delete(array);
    return NULL;
}

CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject_1(cJSON * const object, const char * const name, cJSON *array)
{
    //cJSON *array = cJSON_CreateArray();
    if (add_item_to_object(object, name, array, &global_hooks, false))
    {
        return array;
    }

    cJSON_Delete(array);
    return NULL;
}

将数组转长json 数据格式
int set_potkcheck(unsigned char data[],int len)
{
    int r=0,i;
    std::string ret;
    std::string str;
     char buf[64];
    cJSON *setjson = cJSON_Parse(k_app_setpotkcheck);
    cJSON *message = cJSON_GetObjectItem(setjson, "message");
    cJSON *VALUE = cJSON_GetObjectItem(setjson, "VALUE");
    cJSON_DeleteItemFromObject(message, "VALUE");
    cJSON *json_array = cJSON_CreateArray();
    for (int i = 0; i < len; ++i) {
        cJSON_AddItemToArray(json_array, cJSON_CreateNumber(data[i]));  // 转成10进制数值
    }
    cJSON_AddArrayToObject_1(message, "VALUE",json_array);
    char *modified_json = cJSON_PrintUnformatted(setjson);
    std::string s2=modified_json;

    if (0 == s_factoryCmd.set(modified_json, ret)) {
        printf("%s\n", __func__);
        printf("  - %s\n", modified_json);
        printf("    - %s\n", ret.c_str());

        DEBUG_PRINTF("%s\n", __func__);
        DEBUG_PRINTF("  - %s\n", modified_json);
        DEBUG_PRINTF("    - %s\n", ret.c_str());

        cJSON *json = cJSON_Parse(ret.c_str());
        cJSON *result = cJSON_GetObjectItem(json, "result");
        cJSON *SUCCESS = cJSON_GetObjectItem(result, "SUCCESS");

       if (cJSON_IsNumber(SUCCESS)) {
        printf("SUCCESS: %d\n", SUCCESS->valueint);
       }
        cJSON_Delete(json);
        return SUCCESS->valueint;
    }

     return 1;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baidu_37552881

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值