關於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/zh-hk/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

發表回復

登錄後才能評論