本文目錄一覽:
怎麼用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)。
怎樣用C#把JSON數據轉換為list或數組
引用,在項目中引用
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
==================
定一個跟Json一樣的類
public class CJsonObj
{
//屬性
}
你的Json是文件,就把這文件讀到流裏面來再轉
string jsonText = System.IO..ReadAllText(Json文件); //這就讀出了文件,並存成了string
ListCJsonObj _Test = new ListCJsonObj(); //建立list
//這個是關鍵對象DataContractJsonSerializer
DataContractJsonSerializer _Json = new DataContractJsonSerializer(_Test.GetType());
//轉碼,把string 轉成byte[]數組
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(_JsonText);
//開個流,把數組加進去
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0; //流從0開始
//就這句,讀到List里了
_Test = (ListTest)_Json.ReadObject(_MemoryStream);
//你的JSON文件格式必須與你定的類一致,你懂得
怎麼用 C/C++ 把結構體數組轉成 JSON串
用CSTRING的GetBuffer函數返回一個存放字符的頭指針,用一個CHAR *來接受他,然後用下標操作就可以了。
例如:char * ptr = str.GetBuffer();
定義一個數組來接受各個字符
cahr array[10]=”0″;
char array[0]=ptr[0];
char array[1]=ptr[1];
也可以用一個循環,這樣ARRAY數組就保存了CSTRING中的各個字符.
c語言的cjson怎樣處理轉義
的JSON數據格式有問題,所有Name鍵的值後面少了單引號, 應該修改形如這樣格式的字符
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/287403.html