查看JsonHttpClient的源代码
←
JsonHttpClient
跳转至:
导航
、
搜索
因为以下原因,你没有权限编辑本页:
你刚才请求的操作只对属于该用户组的用户开放:
用户
您可以查看并复制此页面的源代码:
[[Json库]] <pre> // Sample Arduino Json Web Client // Downloads and parse http://jsonplaceholder.typicode.com/users/1 // // Copyright Benoit Blanchon 2014-2016 // MIT License // // Arduino JSON library // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! #include <ArduinoJson.h> #include <SPI.h> #include <Ethernet.h> EthernetClient client; const char* server = "jsonplaceholder.typicode.com"; // server's address const char* resource = "/users/1"; // http resource const unsigned long BAUD_RATE = 9600; // serial connection speed const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server const size_t MAX_CONTENT_SIZE = 512; // max size of the HTTP response // The type of data that we want to extract from the page -- 我们要从此网页中提取的数据的类型 struct UserData { char name[32]; char company[32]; }; // ARDUINO entry point #1: runs once when you press reset or power the board void setup() { initSerial(); initEthernet(); } // ARDUINO entry point #2: runs over and over again forever void loop() { if (connect(server)) { if (sendRequest(server, resource) && skipResponseHeaders()) { char response[MAX_CONTENT_SIZE]; readReponseContent(response, sizeof(response)); UserData userData; if (parseUserData(response, &userData)) { printUserData(&userData); } } disconnect(); } wait(); } // Initialize Serial port void initSerial() { Serial.begin(BAUD_RATE); while (!Serial) { ; // wait for serial port to initialize } Serial.println("Serial ready"); } // Initialize Ethernet library void initEthernet() { byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; if (!Ethernet.begin(mac)) { Serial.println("Failed to configure Ethernet"); return; } Serial.println("Ethernet ready"); delay(1000); } // Open connection to the HTTP server bool connect(const char* hostName) { Serial.print("Connect to "); Serial.println(hostName); bool ok = client.connect(hostName, 80); Serial.println(ok ? "Connected" : "Connection Failed!"); return ok; } // Send the HTTP GET request to the server bool sendRequest(const char* host, const char* resource) { Serial.print("GET "); Serial.println(resource); client.print("GET "); client.print(resource); client.println(" HTTP/1.1"); client.print("Host: "); client.println(server); client.println("Connection: close"); client.println(); return true; } // Skip HTTP headers so that we are at the beginning of the response's body bool skipResponseHeaders() { // HTTP headers end with an empty line char endOfHeaders[] = "\r\n\r\n"; client.setTimeout(HTTP_TIMEOUT); bool ok = client.find(endOfHeaders); if (!ok) { Serial.println("No response or invalid response!"); } return ok; } // Read the body of the response from the HTTP server void readReponseContent(char* content, size_t maxSize) { size_t length = client.readBytes(content, maxSize); content[length] = 0; Serial.println(content); } // Parse the JSON from the input string and extract the interesting values // Here is the JSON we need to parse // { // "id": 1, // "name": "Leanne Graham", // "username": "Bret", // "email": "Sincere@april.biz", // "address": { // "street": "Kulas Light", // "suite": "Apt. 556", // "city": "Gwenborough", // "zipcode": "92998-3874", // "geo": { // "lat": "-37.3159", // "lng": "81.1496" // } // }, // "phone": "1-770-736-8031 x56442", // "website": "hildegard.org", // "company": { // "name": "Romaguera-Crona", // "catchPhrase": "Multi-layered client-server neural-net", // "bs": "harness real-time e-markets" // } // } bool parseUserData(char* content, struct UserData* userData) { // Compute optimal size of the JSON buffer according to what we need to parse. // This is only required if you use StaticJsonBuffer. const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(8) // the root object has 8 elements + JSON_OBJECT_SIZE(5) // the "address" object has 5 elements + JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements + JSON_OBJECT_SIZE(3); // the "company" object has 3 elements // Allocate a temporary memory pool on the stack StaticJsonBuffer<BUFFER_SIZE> jsonBuffer; // If the memory pool is too big for the stack, use this instead: // DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(content); if (!root.success()) { Serial.println("JSON parsing failed!"); return false; } // Here were copy the strings we're interested in strcpy(userData->name, root["name"]); strcpy(userData->company, root["company"]["name"]); // It's not mandatory to make a copy, you could just use the pointers // Since, they are pointing inside the "content" buffer, so you need to make // sure it's still in memory when you read the string return true; } // Print the data extracted from the JSON void printUserData(const struct UserData* userData) { Serial.print("Name = "); Serial.println(userData->name); Serial.print("Company = "); Serial.println(userData->company); } // Close the connection with the HTTP server void disconnect() { Serial.println("Disconnect"); client.stop(); } // Pause for a 1 minute void wait() { Serial.println("Wait 60 seconds"); delay(60000); } </pre> ---- [[Arduino库|返回Arduino库菜单]] [[首页|返回首页]] 更多建议和问题欢迎反馈至 [http://www.yfrobot.com YFRobot论坛]
返回
JsonHttpClient
。
导航菜单
个人工具
登录
名字空间
页面
讨论
不转换
变种
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
台灣正體
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
YF-论坛提问
YFRobot-直营店
YFRobot-企业店
Arduino
Arduino之入门篇
Arduino入门教程
Arduino语法参考
Arduino库
Arduino核心代码
编程平台
Mixly库
Mind+库
MakeCode扩展
传感器系列
积木式传感器系列
黑板传感器系列
蓝板传感器系列
Micro:Bit
Micro:Bit 通用基础教程
Valon智能车
Valon-I
帮助
帮助
wiki语法参考
工具箱
链入页面
相关更改
特殊页面
页面信息