本文目录一览:
- 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函数,以前用过,暂时想不起来,要查一下.