ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

arduino 的 json 测试

2022-05-19 17:03:47  阅读:169  来源: 互联网

标签:arduino doc print json 测试 47 println 40.786 Serial


 源程序

 1 #include <ArduinoJson.h>
 2 String st1,st2,st3;
 3 
 4 const char* cc1;
 5 const char* cc2;
 6 const char* cc3;
 7 int in1,in2,in3,in4;
 8 void setup() {
 9 
10   Serial.begin(115200);
11     
12   DynamicJsonDocument doc(1024);
13  
14   // WARNING: the string in the input  will be duplicated in the JsonDocument.
15   String input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":\"10\"}";
16   deserializeJson(doc, input);
17   JsonObject obj = doc.as<JsonObject>();
18 
19   // You can use a String to get an element of a JsonObject
20   // No duplication is done.
21   long time = obj[String("time")];
22   //
23   //st1=doc["sensor"];  //此句编译不通过,可用以下两句代替,String 对象声明时可用json赋初值,之后不可以;或可采用下面第3句的方法
24   String sensor = doc["sensor"];
25   st1=sensor;
26 
27   st2=doc["data"].as<String>();
28   st3=doc["time"].as<String>();
29 
30   cc1=doc["sensor"];
31   cc2=doc["data"];
32   cc3=doc["time"];
33 
34   in1=doc["time"].as<int>();
35   in2=doc["time"];
36   in3=doc["data"].as<int>();
37   in4=doc["data"];
38   Serial.println();
39   Serial.print("st1:");  Serial.println(st1); 
40   Serial.print("st2:");  Serial.println(st2); 
41   Serial.print("st3:");  Serial.println(st3); 
42   Serial.print("cc1:");  Serial.println(cc1); 
43   Serial.print("cc2:");  Serial.println(cc2); 
44   Serial.print("cc3:");  Serial.println(cc3); 
45   Serial.print("in1:");  Serial.println(in1,10); 
46   Serial.print("in2:");  Serial.println(in2,10); 
47   Serial.print("in3:");  Serial.println(in3,10); 
48   Serial.print("in4:");  Serial.println(in4,10); 
49   
50   double latitude = doc["data"][0];
51   double longitude = doc["data"][1];
52 
53   // Print values.
54   Serial.println(sensor);
55   Serial.println(time);
56   Serial.println(latitude, 6);
57   Serial.println(longitude, 6);
58 
59   obj["sensor"] = "new gps";
60   obj["code"] = 200;
61   // Lastly, you can print the resulting JSON to a String
62   String output;
63   serializeJson(doc, output);
64 
65   Serial.println(output);
66 }
67 
68 void loop() {
69   // not used in this example
70 }
View Code

串口输出:

16:47:40.786 -> st1:gps
16:47:40.786 -> st2:10
16:47:40.786 -> st3:1351824120
16:47:40.786 -> cc1:gps
16:47:40.786 -> cc2:10
16:47:40.786 -> cc3:
16:47:40.786 -> in1:1351824120
16:47:40.786 -> in2:1351824120
16:47:40.786 -> in3:10
16:47:40.786 -> in4:10
16:47:40.786 -> gps
16:47:40.786 -> 1351824120
16:47:40.786 -> 0.000000
16:47:40.786 -> 0.000000
16:47:40.786 -> {"sensor":"new gps","time":1351824120,"data":"10","code":200}

 

标签:arduino,doc,print,json,测试,47,println,40.786,Serial
来源: https://www.cnblogs.com/chengwh/p/16289191.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有