cjsonobject中文的簡單介紹

  • 1、#C JsonObject newObj = new JsonObject(jsonStr); JsonObject需要引用什麼?
  • 2、有哪些 C++ 的 JSON 庫比較好
  • 3、怎麼用C語言獲取JSON中的數據?

using System.Web.Script.Serialization;

JavaScriptSerializer js = new JavaScriptSerializer();

我現在用的是cJSON這個JSON庫,在github上可以下載到,是開源的,Windows和Linux系統開發都可以用。而且是最輕量級的JSON庫,下載下來之後再解壓,只需要將裡面的cJSON.c和cJSON.h這兩個文件複製到你的項目(源碼)目錄就可以了(需要調用cJSON的那個代碼文件要加上一句#include “cJSON.h”)。使用起來也超簡單,通過閱讀cJSON.h中的函數注釋和github上面的說明你就會知道怎麼用。

用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)。

原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126222.html

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

相關推薦

  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python讀取中文

    Python是一種高級編程語言,被廣泛地應用於各種領域中。而處理中文數據也是其中重要的一部分。本文將介紹在Python中如何讀取中文,為大家提供指導和幫助。 一、讀取中文文件 在P…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29
  • jQuery Datatable分頁中文

    jQuery Datatable是一個非常流行的數據表插件,它可以幫助您快速地在頁面上創建搜索、過濾、排序和分頁的數據表格。不過,它的默認設置是英文的,今天我們就來探討如何將jQu…

    編程 2025-04-29
  • Python計算中文字元個數

    本文將從多個方面對Python計算中文字元個數進行詳細的闡述,包括字元串長度計算、正則表達式統計和模塊使用方法等內容。 一、字元串長度計算 在Python中,計算字元串長度是非常容…

    編程 2025-04-29
  • Python3亂碼轉中文

    本文將詳細介紹如何轉換Python3中的亂碼為中文字元,幫助Python3開發工程師更好的處理中文字元的問題。 一、Python3中文亂碼的原因 在Python3中,中文字元使用的…

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

    編程 2025-04-28
  • 從16進位轉義到中文字元

    16進位轉義是為了在不同的字符集、不同的編碼下,能夠保證特殊字元被正確的識別和渲染。本文將從多個方面對16進位轉義做詳細的闡述,讓讀者對其有更深入的了解。 一、轉義實現 在Web開…

    編程 2025-04-28
  • opendistroforelasticsearch-kibana的中文應用

    本文將介紹opendistroforelasticsearch-kibana在中文應用中的使用方法和注意事項。 一、安裝及配置 1、安裝opendistroforelasticse…

    編程 2025-04-28

發表回復

登錄後才能評論