关于c++builderjson的信息

本文目录一览:

如何使用c语言获取文件中的json数据

直接文件操作就行了。fopen,然后直接读出文件中的json数据,保存到一个数组里面就行了

如何使用c builder

新建一个Console Application或者Console Wizard就可以调试C/C++程序了(无需保存工程,可以内存编译),比VC++要简单。

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

cbuilder是什么?它与c++builder是一样的吗?

没有听说过什么c

builder

呀,

Borland

C++

Builder

是Borland

公司开发的用于C/C++开发的IDE(

集成开发环境

1、没有听说过c

builder

2、如果有的话应该是差不多的

IDE,IDE是

编译器

解释器

,编缉器等的集合体

3、按字面解释的话,c

builder

应该只能编译C语言,C++

Builder

有一款开发环境就是Borland

公司的Borland

C++

Builder

,可以编译C/

C++语言

c语言 解析json字符串

你好,你用json-c库,编译通过了吗?我是在ubuntu里使用json-c库,但是无法编译通过,报错 undefined reference to ‘json_tokener_parse’,类似的函数没定义的错误,你是怎么调用的json-c库?请教一下,谢谢!

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/183764.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-25 05:52
下一篇 2024-11-25 05:52

相关推荐

  • Java 监控接口返回信息报错信息怎么处理

    本文将从多个方面对 Java 监控接口返回信息报错信息的处理方法进行详细的阐述,其中包括如何捕获异常、如何使用日志输出错误信息、以及如何通过异常处理机制解决报错问题等等。以下是详细…

    编程 2025-04-29
  • 使用Python爬虫获取电影信息的实现方法

    本文将介绍如何使用Python编写爬虫程序,来获取和处理电影数据。需要了解基本的Python编程语言知识,并使用BeautifulSoup库和Requests库进行爬取。 一、准备…

    编程 2025-04-28
  • Python爬取网页信息

    本文将从多个方面对Python爬取网页信息做详细的阐述。 一、爬虫介绍 爬虫是一种自动化程序,可以模拟人对网页进行访问获取信息的行为。通过编写代码,我们可以指定要获取的信息,将其从…

    编程 2025-04-28
  • 如何使用Python执行Shell命令并获取执行过程信息

    本文将介绍如何使用Python执行Shell命令并获取执行过程信息。我们将从以下几个方面进行阐述: 一、执行Shell命令 Python内置的subprocess模块可以方便地执行…

    编程 2025-04-28
  • Python实现身份信息模拟生成与查验

    本文将从以下几个方面对Python实现身份信息模拟生成与查验进行详细阐述: 一、身份信息生成 身份信息生成是指通过代码生成符合身份信息规范的虚假数据。Python中,我们可以使用f…

    编程 2025-04-27
  • Dapper使用getschema获取表信息

    本文旨在介绍Dapper中使用getschema获取表信息的方法和注意事项。 一、获取某张表的所有列信息 使用Dapper获取某张表信息,可以使用 `IDbConnection.G…

    编程 2025-04-27
  • 已装备我军的空中信息化作战平台

    本文将会从多个方面详细阐述已装备我军的空中信息化作战平台。 一、平台概述 已装备我军的空中信息化作战平台是一个全新的作战系统,具备实时数据采集、处理、分析、共享的能力。它可以在不同…

    编程 2025-04-27
  • 通过提交信息搜索-使用git

    本篇文章重点讲解如何使用git通过提交信息来搜索。我们将从多个方面介绍如何使用git来搜索提交信息,并提供相应的代码示例以供参考。 一、搜索方式 Git提供了三种搜索方式,分别为:…

    编程 2025-04-27
  • Linux查看系统信息

    一、CPU信息 Linux系统下,查看CPU的信息最常用的命令是lscpu。该命令可以显示CPU架构、核心数量、线程数、缓存大小、CPU频率等信息。例如: lscpu 该命令会输出…

    编程 2025-04-24
  • 软考 信息安全工程师

    软考 信息安全工程师是一项技能型国家级资格认证考试,主要测试考生在信息安全领域的理论知识和实践技能,是证明个人信息安全能力的重要证书。本文将从多个方面对软考 信息安全工程师做详细的…

    编程 2025-04-23

发表回复

登录后才能评论