cjson寫入文件,cjson使用

本文目錄一覽:

誰給推薦一個C++的JSON庫

jsoncpp、還有cJSON,推薦cJSON這個輕量級的JSON庫,它使用起來非常簡單,只需要把cJSON.c和cJSON.h兩個文件複製到你的項目中就可以了,使用時#include “cJSON.h”。cJSON可以在github上下載,github上還有一段簡單的使用說明,cJSON庫可以解析JSON,也可以生成JSON文件,用cJSON一般只用寫4、5行代碼(所以說它的輕量級的)。

lua中怎樣將json文件寫入沙盒

解析JSON

local cjson = require “cjson”

local sampleJson = [[{“age”:”23″,”testArray”:{“array”:[8,9,11,14,25]},”Himi”:”himigame.com”}]];

–解析json字符串

local data = cjson.decode(sampleJson);

–打印json字符串中的age字段

print(data[“age”]);

–打印數組中的第一個值(lua默認是從0開始計數)

print(data[“testArray”][“array”][1]);

編碼JSON

local cjson = require “cjson”

local retTable = {}; –最終產生json的表

–順序數值

local intDatas = {};

intDatas[1] = 100;

intDatas[2] = “100”;

–數組

local aryDatas = {};

aryDatas[1] = {};

aryDatas[1][“鍵11”] = “值11”;

aryDatas[1][“鍵12”] = “值12”;

aryDatas[2] = {};

aryDatas[2][“鍵21”] = “值21”;

aryDatas[2][“鍵22”] = “值22”;

–對Table賦值

retTable[“鍵1”] = “值1”;

retTable[2] = 123;

retTable[“int_datas”] = intDatas;

retTable[“aryDatas”] = aryDatas;

–將表數據編碼成json字符串

local jsonStr = cjson.encode(retTable);

print(jsonStr);

–結果是:{“int_datas”:[100,”100″],”2″:123,”鍵1″:”值1″,”aryDatas”:[{“鍵12″:”值12″,”鍵11″:”值11”},{“鍵21″:”值21″,”鍵22″:”值22”}]}。

怎麼用C語言獲取JSON中的數據?

用C語言獲取JSON中的數據的方法是使用 CJSON。

以下簡單介紹用CJSON的思路及實現:

1)創建json,從json中獲取數據。

#nclude stdio.h

#include “cJSON.h”

char * makeJson()

{

cJSON * pJsonRoot = NULL;

pJsonRoot = cJSON_CreateObject();

if(NULL == pJsonRoot)

{

//error happend here

return NULL;

}

cJSON_AddStringToObject(pJsonRoot, “hello”, “hello world”);

cJSON_AddNumberToObject(pJsonRoot, “number”, 10010);

cJSON_AddBoolToObject(pJsonRoot, “bool”, 1);

cJSON * pSubJson = NULL;

pSubJson = cJSON_CreateObject();

if(NULL == pSubJson)

{

// create object faild, exit

cJSON_Delete(pJsonRoot);

return NULL;

}

cJSON_AddStringToObject(pSubJson, “subjsonobj”, “a sub json string”);

cJSON_AddItemToObject(pJsonRoot, “subobj”, pSubJson);

char * p = cJSON_Print(pJsonRoot);

// else use :

// char * p = cJSON_PrintUnformatted(pJsonRoot);

if(NULL == p)

{

//convert json list to string faild, exit

//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free

cJSON_Delete(pJsonRoot);

return NULL;

}

//free(p);

cJSON_Delete(pJsonRoot);

return p;

}

void parseJson(char * pMsg)

{

if(NULL == pMsg)

{

return;

}

cJSON * pJson = cJSON_Parse(pMsg);

if(NULL == pJson)

{

// parse faild, return

return ;

}

// get string from json

cJSON * pSub = cJSON_GetObjectItem(pJson, “hello”);

if(NULL == pSub)

{

//get object named “hello” faild

}

printf(“obj_1 : %s\n”, pSub-valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, “number”);

if(NULL == pSub)

{

//get number from json faild

}

printf(“obj_2 : %d\n”, pSub-valueint);

// get bool from json

pSub = cJSON_GetObjectItem(pJson, “bool”);

if(NULL == pSub)

{

// get bool from json faild

}

printf(“obj_3 : %d\n”, pSub-valueint);

// get sub object

pSub = cJSON_GetObjectItem(pJson, “subobj”);

if(NULL == pSub)

{

// get sub object faild

}

cJSON * pSubSub = cJSON_GetObjectItem(pSub, “subjsonobj”);

if(NULL == pSubSub)

{

// get object from subject object faild

}

printf(“sub_obj_1 : %s\n”, pSubSub-valuestring);

cJSON_Delete(pJson);

}

int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

printf(“%s\n”, p);

parseJson(p);

free(p);//這裡不要忘記釋放內存,cJSON_Print()函數或者cJSON_PrintUnformatted()產生的內存,使用free(char *)進行釋放

return 0;

}

2)創建json數組和解析json數組

//創建數組,數組值是另一個JSON的item,這裡使用數字作為演示

char * makeArray(int iSize)

{

cJSON * root = cJSON_CreateArray();

if(NULL == root)

{

printf(“create json array faild\n”);

return NULL;

}

int i = 0;

for(i = 0; i iSize; i++)

{

cJSON_AddNumberToObject(root, “hehe”, i);

}

char * out = cJSON_Print(root);

cJSON_Delete(root);

return out;

}

//解析剛剛的CJSON數組

void parseArray(char * pJson)

{

if(NULL == pJson)

{

return ;

}

cJSON * root = NULL;

if((root = cJSON_Parse(pJson)) == NULL)

{

return ;

}

int iSize = cJSON_GetArraySize(root);

for(int iCnt = 0; iCnt iSize; iCnt++)

{

cJSON * pSub = cJSON_GetArrayItem(root, iCnt);

if(NULL == pSub)

{

continue;

}

int iValue = pSub-valueint;

printf(“value[%2d] : [%d]\n”, iCnt, iValue);

}

cJSON_Delete(root);

return;

}

有兩種方法:

一是標準的輸出輸入方式 比如新建一個磁盤文件c:\a.txt, 將鍵盤輸入的一字符串寫到文件中:

FILE *ft;

char str[50];

ft=fopen(“c:\\a.txt”,”w+”);

printf(“輸入一個字符串:”);

scanf(“%s”,str);

fputs(str,ft);

fclose(ft);

//重新打開這個文件並讀出字符串,顯示在屏幕上 ft=fopen(“c:\\a.txt”,”rt”);

fgets(str,50,ft);

fclose(ft); printf(“%s”,str);

二是低級輸入輸出方式 仍如上例:

int hd; char str[50]; printf(“輸入一個字符串:”);

scanf(“%s”,str);

hd=open(“c:\\a.txt”,O_CREAT|O_TEXT|O_WRONLY);

write(hd,str,strlen(str));

close(hd); //重新打開這個文件並讀出字符串,顯示在屏幕上。

hd=open(“c:\\a.txt”,O_TEXT|O_RDONLY); read(hd,str,50);

close(hd); printf(“%s”,str)。

原創文章,作者:YHSG,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/133376.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YHSG的頭像YHSG
上一篇 2024-10-03 23:58
下一篇 2024-10-03 23:58

相關推薦

  • vue下載無後綴名的文件被加上後綴.txt,有後綴名的文件下載正常問題的解決

    本文旨在解決vue下載無後綴名的文件被加上後綴.txt,有後綴名的文件下載正常的問題,提供完整的代碼示例供參考。 一、分析問題 首先,需了解vue中下載文件的情況。一般情況下,我們…

    編程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件並生成完整的圖像

    OBJ格式是一種用於表示3D對象的標準格式,通常由一組頂點、面和紋理映射坐標組成。在本文中,我們將討論如何將多個OBJ文件拼接在一起,生成一個完整的3D模型。 一、讀取OBJ文件 …

    編程 2025-04-29
  • 為什麼用cmd運行Java時需要在文件內打開cmd為中心

    在Java開發中,我們經常會使用cmd在命令行窗口運行程序。然而,有時候我們會發現,在運行Java程序時,需要在文件內打開cmd為中心,這讓很多開發者感到疑惑,那麼,為什麼會出現這…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • Python將矩陣存為CSV文件

    CSV文件是一種通用的文件格式,在統計學和計算機科學中非常常見,一些數據分析工具如Microsoft Excel,Google Sheets等都支持讀取CSV文件。Python內置…

    編程 2025-04-29
  • Python zipfile解壓文件亂碼處理

    本文主要介紹如何在Python中使用zipfile進行文件解壓的處理,同時詳細討論在解壓文件時可能出現的亂碼問題的各種解決辦法。 一、zipfile解壓文件亂碼問題的根本原因 在P…

    編程 2025-04-29
  • Python如何導入py文件

    Python是一種開源的高級編程語言,因其易學易用和強大的生態系統而備受青睞。Python的import語句可以幫助用戶將一個模塊中的代碼導入到另一個模塊中,從而實現代碼的重用。本…

    編程 2025-04-29
  • Python合併多個相同表頭文件

    對於需要合併多個相同表頭文件的情況,我們可以使用Python來實現快速的合併。 一、讀取CSV文件 使用Python中的csv庫讀取CSV文件。 import csv with o…

    編程 2025-04-29
  • Python寫文件a

    Python語言是一種功能強大、易於學習、通用並且高級編程語言,它具有許多優點,其中之一就是能夠輕鬆地進行文件操作。文件操作在各種編程中都佔有重要的位置,Python作為開發人員常…

    編程 2025-04-29

發表回復

登錄後才能評論