本文目錄一覽:
- 1、怎麼用C語言獲取JSON中的數據?
- 2、怎樣實現java 中json格式的數據的刪除
- 3、請問json如何追加內容,並且修改,和刪除操作。急~
- 4、c++builder 6.0 裡面的json類在哪
- 5、ajax jsonp請求數據時發生 cannot delete property錯誤
- 6、為什麼cJSON變數會引起內存泄漏
怎麼用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)。
怎樣實現java 中json格式的數據的刪除
定義一個對象包含三個變數int expandNum=1,floor=3,runFlag=1;
然後把sourceObject 分解為對象列表
然後比對對象是否相同,從列表中刪除
然後在輸出為字元串
請問json如何追加內容,並且修改,和刪除操作。急~
json追加內容並且修改和刪除操作示例:
1、增加:
myObj.user=’我是新增的用戶-小明’;
x +=”h1增加後的數據/h1″forin();
2、修改:
myObj.name= “我的網站”;
x +=”h1修改後的數據/h1″forin();
3、刪除:
delete myObj.sites;
x +=”h1刪除後的數據/h1″forin();
擴展資料
JS動態動態創建JSON數據字元串,並且可以刪除添加修改
script type=”text/javascript”
//添加或者修改json數據
function setJson(jsonStr,name,value)
{
if(!jsonStr)jsonStr=”{}”;
var jsonObj = JSON.parse(jsonStr);
jsonObj[name] = value;
return JSON.stringify(jsonObj)
}
//刪除數據
function deleteJson(jsonStr,name)
{
if(!jsonStr)return null;
var jsonObj = JSON.parse(jsonStr);
delete jsonObj[name];
return JSON.stringify(jsonObj)
}
//生成測試
var myjsonStr = setJson(null,”name”,”aaa”);
alert(myjsonStr);
//添加測試
myjsonStr = setJson(myjsonStr,”age”,18);
alert(myjsonStr);
//修改測試
myjsonStr = setJson(myjsonStr,”age”,20);
alert(myjsonStr);
//刪除測試
myjsonStr = deleteJson(myjsonStr,”age”);
alert(myjsonStr);
/script
c++builder 6.0 裡面的json類在哪
BCB6 不帶json的要2010版本的才有,不過建議自己寫一個 用起來更方便
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern “C”
{
#endif
/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item’s string, if type==cJSON_String */
int valueint; /* The item’s number, if type==cJSON_Number */
double valuedouble; /* The item’s number, if type==cJSON_Number */
char *string; /* The item’s name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
/* Supply malloc, realloc and free functions to cJSON */
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_Print(cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON *array);
/* Retrieve item number “item” from array “array”. Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
/* Get item “string” from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You’ll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr();
/* These calls create a cJSON item of the appropriate type. */
extern cJSON *cJSON_CreateNull();
extern cJSON *cJSON_CreateTrue();
extern cJSON *cJSON_CreateFalse();
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray();
extern cJSON *cJSON_CreateObject();
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
/* Append item to the specified array/object. */
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don’t want to corrupt your existing cJSON. */
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
/* Update array items. */
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
#ifdef __cplusplus
}
#endif
#endif
ajax jsonp請求數據時發生 cannot delete property錯誤
在window下刪除不掉這個變數,是因為這個變數的描述中的configurable為false。
你可以使用
Object.getOwnPropertyDescriptor(window,’變數’)
看一下返回的對象中configurable的布爾值,如果時true就可以刪除,如果false就刪除失敗。
針對無法刪除的變數,那就無法刪除了,除非在定義的時候(Object.defineProperty()),規定這個變數的描述為{configurable:true}
為什麼cJSON變數會引起內存泄漏
即便是全局變數,如果是一般變數或是結構體,也不會有內存泄露的問題. 出現內存泄露,應該全局變數是指針,在需要的時候new了,但是不知道什麼時候delete. 比較典型的就是單例模式 instance* GetInstance() { if(!pInstance) { pInstance = new instance; return pInstance; } } 如果真的沒有合適的地方去delete這個全局變數你可以試一下onExit函數,以前用過,暫時想不起來,要查一下.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/160470.html