cjson和jsonc(CJSON)

本文目錄一覽:

python中cj什麼意思

python中cj全稱是cjson。

c語言中,沒有直接的字典,字符串數組等數據結構,所以要藉助結構體定義,處理json。在cjson中,json對象可以是json,可以是字符串,可以是數字。

由於Python語言的簡潔性、易讀性以及可擴展性,在國外用Python做科學計算的研究機構日益增多,一些知名大學已經採用Python來教授程序設計課程。

java怎麼實現JSON打包UDP

java實現JSON打包UDP cJSON支持在C程序中創建和解析JSON數據,其提供多種方法供C程序使用,最直接的是將cJSON.c和cJSON.h加入到C工程中

(1) QJsonObject用於在Qt中創建JSON對象

(2)數據傳輸通過UDP運行

代碼如下

首先在pro文件中加入

QT += network

h文件內容:

首先在pro文件中加入

QT += network

h文件內容:

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include QMainWindow

#includeQtNetwork

namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

QUdpSocket *sender;

QByteArray byteArray;

private slots:

void on_pushButton_clicked();

private:

Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

cpp文件內容:

#include “mainwindow.h”

#include “ui_mainwindow.h”

#includeQJsonObject

#includeQJsonDocument

#includeQDebug

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui-setupUi(this);

sender = new QUdpSocket(this);

QJsonObject rectJson;

rectJson.insert(“Type”,”Rectangle”);

rectJson.insert(“height”,42);

rectJson.insert(“widght”,23);

QJsonDocument rectJsonDoc;

rectJsonDoc.setObject(rectJson);

byteArray = rectJsonDoc.toJson(QJsonDocument::Compact);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::on_pushButton_clicked()

{

QHostAddress address;

address.setAddress(QString(“192.168.230.140”));

sender-writeDatagram(byteArray.data(),byteArray.size(),

address,4444);

}

程序通過端口4444,向IP為192.168.230.140的主機發送JSON數據

C程序如下:

{

int sock_fd;

char rcv_buff[512];

struct sockaddr_in client_addr;

struct sockaddr_in server_addr;

int client_len;

int rcv_num = -1;

if ((sock_fd = socket(AF_INET, SOCK_DGRAM,0)) 0)

{

perror(“socket create error\n”);

exit(1);

}

memset(server_addr,0,sizeof(struct sockaddr_in));

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(4444);

server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

client_len = sizeof(struct sockaddr_in);

if (bind(sock_fd, (struct sockaddr *)server_addr, sizeof(struct sockaddr_in)) 0)

{

perror(“bind socket error.\n”);

exit(1);

}

while (1)

{

/*zero the buff of rvbsm and hvbsm? zhp*/

rcv_num= recvfrom(sock_fd, rcv_buff, sizeof(rcv_buff), 0, (struct sockaddr*)client_addr, client_len);

if (rcv_num0)

{

rcv_buff[rcv_num] = ‘\0’;

printf(“rx bsm num = %d\n”,rcv_num);

//printf();

printf(“%s %u says: %s\n”,inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),rcv_buff);

//parse_UDP_data(rcv_num,rcv_buff);

if(rcv_buff != NULL)

{

cJSON* pJson = cJSON_Parse(rcv_buff);

if(pJson == NULL)

{

printf(“Json Parse failed.\n”);

return 0;

}

cJSON* pSub1 = cJSON_GetObjectItem(pJson,”Type”);

cJSON* pSub2 = cJSON_GetObjectItem(pJson,”height”);

cJSON* pSub3 = cJSON_GetObjectItem(pJson,”widght”);

if(pSub1!=NULL)

{

printf(“Type : %s\n”, pSub1-valuestring);

}

if(pSub2!=NULL)

{

printf(“height : %d\n”, pSub2-valueint);

}

if(pSub3!=NULL)

{

printf(“widght : %d\n”, pSub3-valueint);

}

}

}

else

{

perror(“recv BSM error\n”);

break;

}

}

close(sock_fd);

}

編譯C程序:gcc -I. -lm -o rencode *.c

由於cJSON使用了標準數學庫,所以在編譯中需鏈接其庫文件-lm;

在IP為192.168.230.140的Linux服務器中執行./rencode

輸出結果為:

jquery ajax中都支持哪些返回類型AxmlBhtmlCjsonpDjson?

Ajax = 異步 JavaScript 和 XML,Ajax 是一種用於創建快速動態網頁的技術,通過在後台與服務器進行少量數據交換,Ajax

可以使網頁實現異步更新。這意味着可以在不重新加載整個網頁的情況下,對網頁的某部分進行更新,傳統的網頁(不使用

Ajax)如果需要更新內容,必需重載整個網頁面網頁鏈接

/**

* 說明:

* 通過CORS解決跨域問題

* 並將數據掛接到物體(car01)身上,與panel面板進行數據綁定

* 當溫度25℃時 car01變紅

* 教程:ThingJS教程——數據對接——Ajax對接

* 難度:★★☆☆☆

*/

var app = new THING.App({

url: ”

});

// 定時器

var timer;

app.on(‘load’, function () {

var car = app.query(‘car01’)[0];

// 物體身上創建monitorData對象 用於存儲動態監控數據

car.monitorData = {

‘溫度’: ”

};

updateData(car);

createPanel(car);

})

/** ******************* 以下為ajax數據對接 ********************/

// 服務器程序端 通過設置 Access-Control-Allow-Origin 解決跨域問題

// 更多關於 CORS “跨域資源共享”(Cross-origin resource sharing)的技術細節 請自行搜索

// 請求傳入參數為 { “id”: id }

// 服務器返回的數據格式為 {“state”:”success”,”data”:{“id”:”4967″,”temper”:”15℃”,”humi”:”59%”,”power”:”20kWh”}}

function updateData(obj) {

// 如果網站是 https 接口則對應 https 請求

// 如果網站是 http 接口則對應 http 請求即可

$.ajax({

type: “get”,

url: “”,

data: { “id”: obj.id },

dataType: “json”, // 返回的數據類型 json

success: function (d) {

console.log(d);

var temper = d.data.temper;

// 設置物體身上的監控數據

obj.setAttribute(“monitorData/溫度”, temper);

changeColor(obj);

// 每隔3s 請求一次數據

timer = setTimeout(function () {

updateData(obj)

}, 3000);

}

});

}

// 停止請求數據

function stopUpdate() {

clearTimeout(timer);

}

function createPanel(obj) {

// 界面組件

var panel = new THING.widget.Panel({

titleText: ‘car01溫度’,

hasTitle: true

});

var monitorControl = panel.addBoolean({ ‘isOpen’: true }, “isOpen”).caption(“監控開關”);

// 將物體的monitor對象中的數據 與 panel 進行綁定

panel.add(obj.monitorData, ‘溫度’).name(‘溫度’);

monitorControl.on(‘change’, function (ev) {

if (ev) {

updateData(obj);

}

else {

stopUpdate();

}

})

}

// 如果溫度25 改變顏色

function changeColor(obj) {

var temper = obj.getAttribute(“monitorData/溫度”);

var value = temper.substr(0, temper.indexOf(“℃”));

if (value 25) {

obj.style.color = ‘rgb(255,0,0)’;

}

else {

obj.style.color = null;

}

}

有哪些 C++ 的 JSON 庫比較好

我現在用的是cJSON這個JSON庫,在github上可以下載到,是開源的,Windows和Linux系統開發都可以用。而且是最輕量級的JSON庫,下載下來之後再解壓,只需要將裏面的cJSON.c和cJSON.h這兩個文件複製到你的項目(源碼)目錄就可以了(需要調用cJSON的那個代碼文件要加上一句#include “cJSON.h”)。使用起來也超簡單,通過閱讀cJSON.h中的函數注釋和github上面的說明你就會知道怎麼用。

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

原創文章,作者:UNA35,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130697.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
UNA35的頭像UNA35
上一篇 2024-10-03 23:29
下一篇 2024-10-03 23:29

相關推薦

發表回復

登錄後才能評論