aJson v1.0
================
Copyright (c) 2010, Interactive Matter, Marcus Nowotny
Based on the cJSON Library, 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.
Welcome to aJson.
================
aJson is the attempt to port a complete JSON implementation to Arduino. It is based on the cJSON implementation, reduced in size and removing one or two features:
- The code has very limited support on ATmega168 - there is just not enough memory and
memory fragmentation is a serious problem
- Arrays and Lists are max 255 elements big
- There is no proper Unicode handling in this code
- There is an internal buffer eating up 256 bytes of ram
Most of the limitation will be gone in one of the future releases.
JSON is described best here: http://www.json.org/
It's like XML, but fat-free. You use it to move data around, store things, or just
generally represent your program's state.
JSON is especially useful to exchange data efficiently with e.g. JavaScript, Java, C++,
Processing or anything else
aJson is a library to receive, understand, create or modify JSON strings directly in the
Arduino. JSON is quite a standard, so that is perfect for exchanging data with other
applications. I combination with HTTP it is suitable to implement REST Web Services.
aJson provides functions to parse JSON strings to object models. Handle, search and
create and modify JSON Object structures.
This is some JSON from this page: http://www.json.org/fatfree.html
```javascript
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
```
Parsing JSON
================
To parse such a structure with aJson you simply convert it to a object tree:
```c
aJsonObject* jsonObject = aJson.parse(json_string);
```
(assuming you got the JSON string in the variable json_string - as a char*)
This is an object. We're in C. We don't have objects. But we do have structs.
Therefore the objects are translated into structs, with all the drawbacks it brings.s
Now we can e.g. retrieve the value for name:
```c
aJsonObject* name = aJson.getObjectItem(root, "name");
```
The value of name can be retrieved via:
```c
Serial.println(name->valuestring);
```
Note that the aJsonObject has a union which holds all possible value types as
overlays - you can get only useful data for the type which you have at hand. You can get
the type as
```c
name->type
```
which can be either aJson_False, aJson_True, aJson_NULL, aJson_Number, aJson_String, aJson_Array
or aJson_Object. For aJson_Number you can use value.number.valueint or value.number.valuedouble, for aJson_String
you can use value.valuestring, for True or False, you can use value.valuebool.
To render the object back to a string you can simply call
```c
char *json_String=aJson.print(jsonObject);
```
Finished? Delete the root (this takes care of everything else).
```c
aJson.deleteItem(root);
```
This deletes the objects and all values referenced by it.
Parsing streams
--------------
As you can see this will eat up lots of memory. Storing the original string and the JSON object is a bit too much
for your Arduino - it will most likely use up all the memory. Therefore it is better to parse streams instead of strings.
A stream in C is a FILE* - on Arduino there are some special streams, but later adapters will be provided.
So if you for example read from a FILE* stream you can simply call
```c
aJsonObject* jsonObject = aJson.parse(file);
```
By that you will not have to store the JSON string in memory.
Filtering while parsing
--------------
Any JSON respond can have object name/value pairs your code either does not understand or is not interested in.
To avoid those values going into your memory you can simply add filters to your parsing request.
A set of filter is just a list of names you are interested in, ended by a null value. If you are
only interested in "name", "format", "height" and "width" in the above example you can do it like:
```c
char** jsonFilter = {"name,"format","height","width",NULL};
aJsonObject* jsonObject = aJson.parse(json_string,jsonFilter);
```
(assuming you got the JSON string in the variable json_string - as a char*)
By that only the following structure is parsed - the rest will be ignored:
```javascript
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"width": 1920,
"height": 1080,
}
}
```
It is good practice to always use the filtering feature to parse JSON answers, to avoid unknown objects swamping your
memory.
Creating JSON Objects from code
================
If you want to see how you'd build this struct in code?
```c
aJsonObject *root,*fmt;
root=aJson.createObject();
aJson.addItemToObject(root, "name", aJson.createItem("Jack (\"Bee\") Nimble"));
aJson.addItemToObject(root, "format", fmt = aJson.createObject());
aJson.addStringToObject(fmt,"type", "rect");
aJson.addNumberToObject(fmt,"width", 1920);
aJson.addNumberToObject(fmt,"height", 1080);
aJson.addFalseToObject (fmt,"interlace");
aJson.addNumberToObject(fmt,"frame rate", 24);
```
The root object has: Object Type and a Child
The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling:
Sibling has type Object, name "format", and a child.
That child has type String, name "type", value "rect", and a sibling:
Sibling has type Number, name "width", value 1920, and a sibling:
Sibling has type Number, name "height", value 1080, and a sibling:
Sibling hs type False, name "interlace", and a sibling:
Sibling has type Number, name "frame rate", value 24
If you want to create an array it works nearly the same way:
```c
aJsonObject* root = aJson.createArray();
aJsonObject* day;
day=aJson.createItem("Monday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Tuesday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Wednesday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Thursday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Friday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Saturday");
aJson.addItemToArray(root, day);
day=aJson.createItem("Sunday");
aJson.addItemToArray(root, day);
```
The whole library (nicely provided by cJSON) is optimized for easy usage. You can create and modify
the object as easy as possible.
aJson Data Structures
================
aJson stores JSON objects in struct objects:
```c
// The aJson structure:
typedef struct aJsonObject {
char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem
struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the i
没有合适的资源?快使用搜索试试~ 我知道了~
ESP8266与物联网
共72个文件
ino:23个
h:18个
cpp:11个
需积分: 37 16 下载量 74 浏览量
2018-11-04
11:24:01
上传
评论 2
收藏 118KB ZIP 举报
温馨提示
用ESP8266 wifi模块实现手机和单片机(arduino)之间的通讯。
资源推荐
资源详情
资源评论
收起资源包目录
小实验:ESP8266连贝壳物联网库文件.zip (72个子文件)
bigiotArduino-master
libraries
ESP8266WiFi
src
ESP8266WiFiSTA.cpp 17KB
WiFiServer.cpp 4KB
ESP8266WiFiGeneric.cpp 13KB
WiFiClient.h 4KB
ESP8266WiFi.h 3KB
ESP8266WiFiGeneric.h 4KB
WiFiClient.cpp 7KB
ESP8266WiFiScan.cpp 9KB
WiFiClientSecure.h 2KB
WiFiServer.h 2KB
ESP8266WiFiMulti.h 2KB
WiFiClientSecure.cpp 15KB
ESP8266WiFiType.h 4KB
WiFiUdp.h 5KB
WiFiUdp.cpp 6KB
ESP8266WiFiAP.h 2KB
ESP8266WiFiAP.cpp 10KB
ESP8266WiFi.cpp 3KB
ESP8266WiFiSTA.h 3KB
ESP8266WiFiScan.h 2KB
ESP8266WiFiMulti.cpp 8KB
include
ssl.h 20KB
ClientContext.h 10KB
wl_definitions.h 3KB
UdpContext.h 9KB
slist.h 599B
keywords.txt 1KB
examples
WiFiAccessPoint
WiFiAccessPoint.ino 2KB
WiFiClientBasic
WiFiClientBasic.ino 1KB
WiFiClientEvents
WiFiClientEvents.ino 887B
WiFiTelnetToSerial
WiFiTelnetToSerial.ino 3KB
WiFiWebServer
WiFiWebServer.ino 2KB
WiFiScan
WiFiScan.ino 1KB
WiFiClient
WiFiClient.ino 2KB
WiFiMulti
WiFiMulti.ino 790B
HTTPSRequest
HTTPSRequest.ino 2KB
NTPClient
NTPClient.ino 5KB
library.properties 511B
aJson
Examples
Json_Serial
Json_Serial.ino 2KB
MultiLevelParsing
MultiLevelParsing.ino 2KB
Json_Example
Json_Example.ino 7KB
utility
stringbuffer.h 1KB
stringbuffer.c 3KB
keywords.txt 2KB
aJSON.cpp 26KB
README.md 10KB
aJSON.h 9KB
.gitignore 89B
library.json 298B
examples
ESP8266
ESP8266WIFI
control4
control4.ino 4KB
NodeMCU
hongwai
hongwai.lua 1KB
init.lua 390B
kaiguan
kaiguan.lua 1KB
init.lua 388B
touchuan
main.lua 1KB
init.lua 251B
webconfig
main.lua 592B
init.lua 251B
AT
kaiguan
kaiguan.ino 3KB
AT_keepOline
AT_keepOline.ino 3KB
config_v1.0__
config_v1.0__.ino 2KB
ESP8266透传设置命令.txt 129B
config_v0.9.5.2_
ESP8266透传设置命令.txt 111B
config_v0.9.5.2_.ino 2KB
test
Arduino串口监视器调试设置ESP8266.txt 292B
test.ino 942B
wendu
wendu.ino 4KB
W5100
kaiguan
kaiguan.ino 3KB
fengming
fengming.ino 3KB
wendu
wendu.ino 4KB
README.md 97B
.gitignore 5B
共 72 条
- 1
资源评论
学长很忙
- 粉丝: 72
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功