前言
在Linux应用开发中,Linux设备和服务器通信时,两者之间数据的传输通常采用JSON数据格式来作为载体,便于两者之间的数据交互。当设备端接收到服务端下发的JSON数据,设备端需要对JSON格式的数据进行解析;当设备端需要将设备的各项状态数据参数上传到服务器时,则需要各项状态数据参数打包成Json格式的数据包。故引入Json-c库来完成Json格式数据的解析和Json格式数据打包。
-
下载Json-c源码
https://s3.amazonaws.com/json-c_releases/releases/index.html -
选择Json-c版本
-
解压到Ubuntu系统后,进入解压后的文件夹进行编译配置与安装。
(1)可配置Json-c生成后的文件保存路径
PS:如无特定路径需求,建议不配置此项,这将保持默认路径(/usr/lib)
./configure --prefix=指定Json-c编译后的文件保存路径
(2)可根据目标平台(x86平台,Arm平台)的不同,配置对应的交叉编译工具链。默认编译工具链为gcc
./configure CC=交叉编译器名称(已添加至环境变量)
PS:配置完成后,也可在生成的Makefile文件中,更改配置内容
(3)执行make进行编译
make
(4)执行make install完成库的安装,
(5)完成后可在配置的路径中找到生成的头文件和库文件
(4)配置库链接器
由于我们在编译配置时,选择了指定的json库安装路径,并没有安装到默认路径/usr/lib下,所以需要在ld库链接器中添加Json-c库的所在路径
vim /etc/ld.so.conf #在新的一行中加入库文件所在目录
- JSON库的使用
(1)如果是QT项目编译,在.pro项目文件中添加库路径和头文件路径
(2)如果是命令行编译:在编译选项中添加Json-c库的路径
gcc test.c -L 指定Json-c库路径 -I 指定Json-c头文件的路径
Demo
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "json-c/json.h"
#include "json-c/json_object.h"char recvJson_buf[2*1024*1024+64] = "{ \"RequestCode\": \"PARAMSCHECK\", \"DeviceNum\": \"3002014833\", \"Params\": { \"WorkMode\": \"MixCount\", \"Currency\": \"RUB\", \"BankSet\": { \"NetParaSet\": { \"Relink\": \"2\", \"TxTimeOut\": \"0\", \"USave\": \"90\", \"ActivePkg\": \"500\", \"TimeSyncCycle\": \"30\" }, \"NetSW\": { \"NetworkSW\": \"1\", \"RejUpload\": \"1\", \"DenoLimit\": \"1\", \"QRPrt\": \"0\", \"DiscRejUpload\": \"1\", \"FSErrCode\": \"1\", \"LoginSW\": \"0\" } }, \"WorkModeSet\": { \"ModeSet\": { \"MultiSort\": \"0\", \"SortATM\": \"0\", \"Count\": \"0\", \"MixCount\": \"1\", \"Version\": \"0\", \"AutoVer\": \"0\", \"UpDown\": \"0\", \"FaceRear\": \"0\", \"Unfit\": \"0\" }, \"PocketPreset\": { \"Stacker1\": \"100\", \"Stacker2\": \"100\", \"Stacker3\": \"0\", \"Stacker4\": \"0\", \"Stacker5\": \"0\", \"Stacker6\": \"0\", \"Stacker7\": \"0\", \"Stacker8\": \"0\", \"RejPort1\": \"100\", \"RejPort2\": \"0\" } }, \"SortSet\": { \"USD\": { \"Val_100\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_50\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_20\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_10\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_5\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_2\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_1\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" } }, \"EUR\": { \"Val_500\": { \"SortLevel\": \"1\", \"ATMLevel\": \"4\", \"FitLevel\": \"2\" }, \"Val_200\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_100\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_50\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_20\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_10\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_5\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" } }, \"GBP\": { \"Val_50\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_20\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_10\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_5\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" } }, \"CHF\": { \"Val_1000\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_200\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_100\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_50\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_20\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" }, \"Val_10\": { \"SortLevel\": \"0\", \"ATMLevel\": \"0\", \"FitLevel\": \"0\" } } }, \"MCtrlSet\": { \"AuthenSW\": { \"DiscriMasterSW\": \"1\", \"IRSW\": \"1\", \"PenmarkSW\": \"1\", \"FluorSW\": \"1\", \"MagnetSW\": \"1\", \"ThickSW\": \"1\", \"DamageSW\": \"0\", \"DogearSW\": \"1\", \"FullSpeedSW\": \"0\", \"TakeNoteSW\": \"0\", \"Position4DualSW\": \"0\", \"PlasticsSW\": \"0\", \"AddSW\": \"0\", \"AutoRotationSW\": \"0\" }, \"SpeedDebug\": { \"FeedingMotor\": \"0\", \"RejNoteMotor\": \"0\", \"DriveMotor\": \"1\", \"Pocket1TurnPlate\": \"1\", \"Pocket2TurnPlate\": \"0\", \"ImpellerMotor\": \"0\", \"AgingTest\": \"1\", \"FeedingMotorPWM\": \"272\", \"RejNoteMotorPWM\": \"277\", \"DriveMotorPWM\": \"2300\" }, \"MotorSet\": { \"FeedingMotorType\": \"IRM\", \"RejNoteMotorType\": \"IRM\", \"MTransMotorType\": \"IRM\", \"FeedingSpeed\": \"IRM\", \"DutyRatio\": \"1:1.0\" } }, \"MagSet\": { \"ForeignCCY\": { \"SMag\": \"5\", \"MSMag\": \"6\", \"MMag\": \"5\", \"Reserv\": \"6\" } }, \"ThickSet\": { \"ThickThreshold\": { \"LThk1\": \"1804\", \"LThk2\": \"1606\", \"LThk3\": \"1507\", \"LThk4\": \"1525\", \"LThk5\": \"1517\", \"LThk6\": \"1821\", \"RThk1\": \"1802\", \"RThk2\": \"1403\", \"RThk3\": \"1532\", \"RThk4\": \"1824\", \"RThk5\": \"1623\", \"RThk6\": \"1866\", \"Dogear\": \"0\", \"AbnPt\": \"12\", \"ThresholdSW\": \"0\" } }, \"ImgSet\": { \"CorrectionSW\": \"0\", \"ImgSaveSW\": \"0\", \"NewOldSW\": \"0\", \"ParaDefaultSW\": \"0\", \"TenderSW\": \"1\", \"ImgDiscriSW\": \"1\", \"OldVerSW\": \"1\", \"Info1SW\": \"0\", \"Info2SW\": \"0\", \"ProgTimeTestSW\": \"0\", \"DistributeInfoSW\": \"0\", \"SmallDenoSW\": \"1\", \"ImgStudySW\": \"0\", \"CommemoraSW\": \"0\", \"DimensionSW\": \"1\", \"VisLtRefGSW\": \"1\", \"VisLtPerspGSW\": \"1\", \"IRReflGSW\": \"1\", \"IRPerspG\": \"1\", \"FluorGSW\": \"1\", \"GraphMagSW\": \"1\", \"MetalThdMagSW\": \"1\", \"OptVarPrtGSW\": \"1\", \"OptMetalThdFeaSW\": \"0\", \"FineHollowGSW\": \"1\", \"EFeatureSW\": \"0\", \"SpectralAbsFeaSW\": \"1\", \"TranspWinFeaSW\": \"1\", \"WaterMarkSW\": \"1\", \"SerialNoSW\": \"1\", \"ThicknessSW\": \"1\", \"OtherFeaSW\": \"0\", \"UnfitSW\": \"1\", \"ImgDiscriRejSW\": \"1\", \"ULDiscRejSW\": \"1\", \"ThickFinancialStandardSW\": \"0\" }, \"SystemInfo\": { \"MachNo\": \"3002014833\", \"MCtrlVer\": \"ZQ3201__APP:2024072301__BOOT:V1__LOGIC:20180004\", \"MagVer\": \"CNY_L_GA3201_240729_00_B4\", \"ThickVer\": \"APP:TH31_20231220_00_ZJ BOOT:200423_01\", \"LCDVer\": \"V5.0_240801_R0\", \"PluginVer\": \"PLG_V4.0_240711_R0\", \"NetVer\": \"V4.0_NET:240717_00\", \"CommVer\": \"COMMLAYER_VER_240722R0\", \"LogicVer\": \"V_FPGA:00290076_20220713\", \"ImgVer\": \"IMG:QFJV20240731-2569\", \"OCRVer\": \"VOCR:240731_D0\", \"Emmc\": \"EMMC:P3-72_P4-9_U-0\" } } }";
int main()
{struct json_object *json_parse;struct json_object *recvObj_buf;recvObj_buf = json_tokener_parse(recvJson_buf);json_object_object_get_ex(recvObj_buf,"RequestCode", &json_parse);printf("object: %s\n", json_object_get_string(json_parse));json_object_object_get_ex(recvObj_buf,"DeviceNum", &json_parse);printf("object: %s\n", json_object_get_string(json_parse));return 0;
}