学习Json记录

第一次写csdn,记录一下。

学习EasyDevice时,了解到EasyDevice使用HTTP协议承载Json数据在设备与服务器之间、及服务器与客户端之间进行数据交互,同时也看到许多网络协议也是基于Json格式的,逐认识到Json应用的广泛性,看来得认识下Json了。

来看下较W3C的教程

JSON:JavaScript 对象表示法(JavaScript Object Notation)。

JSON 是存储和交换文本信息的语法。类似 XML。

JSON 比 XML 更小、更快,更易解析。


JSON 语法是 JavaScript 语法的子集。

JSON 语法规则

JSON 语法是 JavaScript 对象表示法语法的子集。

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 花括号保存对象
  • 方括号保存数组

JSON 名称/值对

JSON 数据的书写格式是:名称/值对。

名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:

"firstName" : "John"

这很容易理解,等价于这条 JavaScript 语句:

firstName = "John"

JSON 值

JSON 值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null

JSON 对象

JSON 对象在花括号中书写:

对象可以包含多个名称/值对:

{ "firstName":"John" , "lastName":"Doe" }

这一点也容易理解,与这条 JavaScript 语句等价:

firstName = "John"
lastName = "Doe"

JSON 数组

JSON 数组在方括号中书写:

数组可包含多个对象:

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。



自己试着写一个Json文件!

==================================  example1_dvr.json ==================================
{
	"System" : {
		"Hardware" : "DVR_HW_v1.0",
		"Software" : "DVR_SW_v1.0",
		"CameraNum" : 4,
		"HasPtz" : true,
		"HasEthernet" : true,
		"HasWiFi" : false,
		"HasGPS" : true
	},
	"Config" : {
		"Camera" : [
			{
				"Video" : {
					"EncType" : "H264",
					"EncSize" : "1280x960",
					"EncFrameRate" : 29.5,
					"EncMode" : "CBR",
					"EncBitRate" : 2048
				},
				"Audio" : {
					"EncType" : "G711A"
				}
			},
			{
				"Video" : {
					"EncType" : "H264",
					"EncSize" : "1280x960",
					"EncFrameRate" : 29.5,
					"EncMode" : "CBR",
					"EncBitRate" : 2048
				},
				"Audio" : {
					"EncType" : "G711A"
				}
			},
			{
				"Video" : {
					"EncType" : "H264",
					"EncSize" : "1280x960",
					"EncFrameRate" : 29.5,
					"EncMode" : "CBR",
					"EncBitRate" : 2048
				},
				"Audio" : {
					"EncType" : "G711A"
				}
			},
			{
				"Video" : {
					"EncType" : "H264",
					"EncSize" : "1280x960",
					"EncFrameRate" : 29.5,
					"EncMode" : "CBR",
					"EncBitRate" : 2048
				},
				"Audio" : {
					"EncType" : "G711A"
				}
			}
		],
		"PTZ" : {
			"Serial" : "/dev/ttyAMA2",
			"Baudrate" : 9600,
			"Protocol" : "PolcoD",
			"CameraAddr" : [1, 2, 3, 4]
		},
		"GPS" : {
			"Serial" : "/dev/ttyAMA1",
			"Baudrate" : 9600,
			"Protocal" : "Blox"
		}
	},
	"Log" : [
		{"Time" : "09:26:23", "Tag" : "UserLogin", "Description" : "ip:183.39.231.137"},
		{"Time" : "09:37:30", "Tag" : "UserLogout", "Description" : "ip:183.39.231.137"},
		{"Time" : "09:51:36", "Tag" : "UserLogin", "Description" : "ip:121.196.220.153"},
		{"Time" : "09:52:32", "Tag" : "UserLogout", "Description" : "ip:121.196.220.153"},
		{"Time" : "11:56:18", "Tag" : "UserLogin", "Description" : "ip:121.196.220.153"},
		{"Time" : "11:56:53", "Tag" : "UserLogout", "Description" : "ip:121.196.220.153"},
		{"Time" : "19:26:23", "Tag" : "UserLogin", "Description" : "ip:183.39.231.137"},
		{"Time" : "19:37:30", "Tag" : "UserLogout", "Description" : "ip:183.39.231.137"},
		{"Time" : "19:51:36", "Tag" : "UserLogin", "Description" : "ip:121.196.220.153"},
		{"Time" : "21:56:53", "Tag" : "UserLogout", "Description" : "ip:121.196.220.153"}
	]
}
================================== end ==================================

在网上找个在线Json解析网站,将上述Json贴上去,验证通过^_^!!!

使用cJson库解析再打印下

==================================  example1_dvr.c ==================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <cJSON.h>

int main(int argc, char *argv[])
{
	FILE *fp = fopen(argv[1], "r");
	char *pJsonFileBuf = NULL;
	int iJsonFileLen = 0;

	cJSON *pJsonObj = NULL;
	char *pJsonPrintBuf = NULL;

	if (!fp) {
		fprintf(stderr, "fopen fail: %s\n", strerror(errno));
		return -1;
	}

	fseek(fp, 0L, SEEK_END);
	iJsonFileLen = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	pJsonFileBuf = (char*) calloc(1, iJsonFileLen + 1);
	if (!pJsonFileBuf) {
		fprintf(stderr, "Calloc for JsonFileBuf fail: %s\n", strerror(errno));
		return -1;
	}
	fread(pJsonFileBuf, iJsonFileLen, 1, fp);

	pJsonObj = cJSON_Parse(pJsonFileBuf);
	if (!pJsonObj) {
		fprintf(stderr, "cJSON_Parse fail: %s\n", cJSON_GetErrorPtr());
		return -1;
	}

	pJsonPrintBuf = cJSON_Print(pJsonObj);
	if (!pJsonPrintBuf) {
		fprintf(stderr, "cJSON_Print fail: %s\n", cJSON_GetErrorPtr());
		return -1;
	}

	fprintf(stderr, "JSON format is right !!!\n");
	printf("%s", pJsonPrintBuf);fflush(stdout);
	fprintf(stderr, "\n");

	free(pJsonPrintBuf);
	cJSON_Delete(pJsonObj);
	free(pJsonFileBuf);
	fclose(fp);
	return -1;
}
==================================  end ==================================


再来个运行截图

解析正确,cJson打印格式跟手写不一样哦。先学到这吧,用到再继续熟悉,哈哈


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值