经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
用OkHttpGo和FastJson获取OneNET云平台数据(解析嵌套数组)
来源:cnblogs  作者:stuMartin  时间:2021/3/1 8:42:01  对本文有异议

JSON数据格式有两种,一种是 { } 大括号表示的JSON对象,一种是 [ ] 中括号表示的JSON数组。从OneNET获取到的数组是这样的,并用Json解析网址查看https://jsonformatter.curiousconcept.com/#(图1)。可以看到这是一个对象包含了数组又包含对象的JSON数据。比如我现在要获取id号为3300_1_5700时间为2020-12-09 20:19:30.624的数据(图1唯一展开的datastreams数组元素)它是一个JSON对象(因为它是被一个大括号包者的),所以datastreams[0]这个JSON对象时包含了datapoints和id,前者又是一个JSON数组(中括号包者),后者则是一个普通的字符串。综上,要获取图1的value值,我要通过一开始返回到的JSON数据 找到 dataJSON对象 找到 datastreamsJSON数组 找到 datapointsJSON数组 中的value字符串。 

```

{ "errno": 0, "data": { "count": 10, "datastreams": [ { "datapoints": [ { "at": "2020-12-09 20:19:30.624", "value": 0 } ], "id": "3300_1_5700" }, { "datapoints": [ { "at": "2019-07-30 21:38:03.729", "value": 0 } ], "id": "3327_0_5700" }, { "datapoints": [ { "at": "2019-12-26 21:27:14.700", "value": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000" } ], "id": "3336_0_5514" }, { "datapoints": [ { "at": "2019-12-26 21:27:14.700", "value": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000" } ], "id": "3336_0_5515" }, { "datapoints": [ { "at": "2020-12-09 20:19:30.624", "value": 0 } ], "id": "3303_0_5700" }, { "datapoints": [ { "at": "2019-04-21 14:35:11.158", "value": 29.8 } ], "id": "3304_0_5700" }, { "datapoints": [ { "at": "2020-12-09 20:19:31.980", "value": 0 } ], "id": "3326_0_5700" }, { "datapoints": [ { "at": "2019-12-26 19:23:09.191", "value": false } ], "id": "3338_0_5850" }, { "datapoints": [ { "at": "2020-12-09 20:19:29.412", "value": 0 } ], "id": "3300_0_5700" }, { "datapoints": [ { "at": "2019-05-17 09:09:13.681", "value": false } ], "id": "3311_0_5850" } ] }, "error": "succ" }

```

 

            (图1)

执行过程是,当获取到data JSON对象时,获取count字符串,这是OneNET平台给我返回的数值,功能时告诉我datastreams中一共有多少个元素,所以用遍历datastreams数组,进入datastreams后,获取id号,id号是用单片机根据NB-IoT文档(OneNET官网文档)上传到云平台的,根据这一id号来确认到底是哪一个传感器。总之什么时候用getJSONObject或getJSONaRRAY一定要看自己返回的数据是什么,是大括号还是中括号,如果都不是只是一个普通的字符串就用getString方法获取字符串,getString("value")中""写的是元素名,拿的是元素值,比如"value":0这一行,拿的就是0这个元素值。

  1. 1 package com.example.helloworld.learnokhttp;
  2. 2
  3. 3 import android.bluetooth.BluetoothClass;
  4. 4 import android.os.Bundle;
  5. 5 import android.view.View;
  6. 6 import android.widget.Button;
  7. 7 import android.widget.TextView;
  8. 8 import android.widget.Toast;
  9. 9
  10. 10 import androidx.annotation.Nullable;
  11. 11 import androidx.appcompat.app.AppCompatActivity;
  12. 12
  13. 13 import com.alibaba.fastjson.JSON;
  14. 14 import com.alibaba.fastjson.JSONArray;
  15. 15 import com.alibaba.fastjson.JSONObject;
  16. 16 import com.example.helloworld.R;
  17. 17 import com.example.helloworld.learnokhttp.javabean.Data;
  18. 18 import com.example.helloworld.learnokhttp.javabean.Datapoints;
  19. 19 import com.example.helloworld.learnokhttp.javabean.Datastreams;
  20. 20 import com.example.helloworld.learnokhttp.javabean.JsonRootBean;
  21. 21 import com.lzy.okgo.OkGo;
  22. 22 import com.lzy.okgo.adapter.Call;
  23. 23 import com.lzy.okgo.cache.CacheMode;
  24. 24 import com.lzy.okgo.callback.StringCallback;
  25. 25 import com.lzy.okgo.model.Response;
  26. 26
  27. 27 import org.w3c.dom.Text;
  28. 28
  29. 29 import java.util.Iterator;
  30. 30 import java.util.List;
  31. 31
  32. 32 public class OkHttpGoActivity extends AppCompatActivity implements View.OnClickListener {
  33. 33
  34. 34 private String url = "https://api.heclouds.com/devices/523698851/datapoints";//
  35. 35 private Button btn_get,btn_resolvebyfastjson;
  36. 36 private TextView tv_result,tv_wendu,tv_do,tv_ph,tv_tds,tv_zhuodu;
  37. 37
  38. 38 @Override
  39. 39 protected void onCreate(@Nullable Bundle savedInstanceState) {
  40. 40 super.onCreate(savedInstanceState);
  41. 41 setContentView(R.layout.activity_okhttpgo);
  42. 42
  43. 43 setTitle("用封装的方法获取数据");
  44. 44
  45. 45 btn_get = findViewById(R.id.btn_get);
  46. 46 btn_resolvebyfastjson = findViewById(R.id.btn_resolvebyfastjson);
  47. 47 tv_result = findViewById(R.id.tv_result);
  48. 48 tv_wendu = findViewById(R.id.tv_wendu);
  49. 49 tv_do = findViewById(R.id.tv_do);
  50. 50 tv_ph = findViewById(R.id.tv_ph);
  51. 51 tv_tds = findViewById(R.id.tv_tds);
  52. 52 tv_zhuodu = findViewById(R.id.tv_zhuodu);
  53. 53 btn_get.setOnClickListener(this);
  54. 54 btn_resolvebyfastjson.setOnClickListener(this);
  55. 55 }
  56. 56
  57. 57 @Override
  58. 58 protected void onDestroy() {
  59. 59 super.onDestroy();
  60. 60 //Activity销毁时,取消网络请求
  61. 61 OkGo.getInstance().cancelTag(this);
  62. 62 }
  63. 63
  64. 64 /**
  65. 65 *先用okhttpgo拿一次数据
  66. 66 *@author home
  67. 67 *@time 2021/2/24 10:23
  68. 68 */
  69. 69 private void getByOkGo() {
  70. 70 OkGo.<String>get(url)//文档说的第一行泛型一定要添加是指这里
  71. 71 .headers("api-key", "4VdbaFeRQZRwaSTWNhWxb2UEHaw=")//设备的api-key
  72. 72 .headers("Content-Type","application/json")
  73. 73 .tag(this)
  74. 74 .execute(new StringCallback() {
  75. 75 @Override
  76. 76 public void onSuccess(Response<String> response) {//请求成功回调onSuccess方法
  77. 77 tv_result.setText(response.body());//文档中有说明,body是返回的数据
  78. 78 }
  79. 79
  80. 80 @Override
  81. 81 public void onError(Response<String> response) {
  82. 82 Toast.makeText(getApplicationContext(), "接口请求错误!", Toast.LENGTH_LONG).show();
  83. 83 }
  84. 84 });
  85. 85 }
  86. 86
  87. 87 /**
  88. 88 *OkHttpGo与FastJson结合,并显示到界面
  89. 89 *@author home
  90. 90 *@time 2021/2/24 10:25
  91. 91 */
  92. 92 private void jsonToJavaListByFastJson() {
  93. 93 OkGo.<String>get(url)//文档说的第一行泛型一定要添加是指这里
  94. 94 .headers("api-key", "4VdbaFeRQZRwaSTWNhWxb2UEHaw=")//设备的api-key
  95. 95 .headers("Content-Type","application/json")
  96. 96 .tag(this)
  97. 97 .execute(new StringCallback() {
  98. 98 @Override
  99. 99 public void onSuccess(Response<String> response) {//请求成功回调onSuccess方法
  100. 100
  101. 101 String value = "", id = "";
  102. 102 int count = 0;
  103. 103
  104. 104 JSONObject jsonObjectData = JSONObject.parseObject(response.body()).getJSONObject("data");//获取json对象后再获取json对象,即第二层data
  105. 105 count = jsonObjectData.getIntValue("count");//count是Datastreams数组的下标值
  106. 106 JSONArray jsonArrayDatastreams = jsonObjectData.getJSONArray("datastreams");//获取json数组即datastearms
  107. 107
  108. 108 for(int j = 0; j < count; j++) {//遍历Datastreams数组
  109. 109 JSONObject jsonObjectIndex = jsonArrayDatastreams.getJSONObject(j);
  110. 110 id = jsonObjectIndex.getString("id");
  111. 111 JSONArray jsonArrayDatapoints = jsonObjectIndex.getJSONArray("datapoints");
  112. 112 JSONObject jsonObjectValue = jsonArrayDatapoints.getJSONObject(0);//Datapoints数组只有一个元素(对象),所以下标是1
  113. 113 value = jsonObjectValue.getString("value");
  114. 114 switch (id) {
  115. 115 case "3303_0_5700": //温度
  116. 116 //System.out.println("温度" + value + id);
  117. 117 tv_wendu.setText("温度" + value + "\t设备号" + id);
  118. 118 break;
  119. 119 case "3300_0_5700": //溶解氧
  120. 120 //System.out.println("DO" + value + id);
  121. 121 tv_do.setText("溶解氧" + value + "\t设备号" + id);
  122. 122 break;
  123. 123 case "3327_0_5700": //电导率
  124. 124 //System.out.println("电导率" + value + id);
  125. 125 tv_tds.setText("TDS" + value + "\t设备号" + id);
  126. 126 break;
  127. 127 case "3326_0_5700": //ph
  128. 128 //System.out.println("ph" + value + id);
  129. 129 tv_ph.setText("PH" + value + "\t设备号" + id);
  130. 130 break;
  131. 131 case "3300_1_5700": //浊度
  132. 132 //System.out.println("浊度" + value + id);
  133. 133 tv_zhuodu.setText("浊度" + value + "\t设备号" + id);
  134. 134 break;
  135. 135 }
  136. 136 }
  137. 137 }
  138. 138
  139. 139 @Override
  140. 140 public void onError(Response<String> response) {
  141. 141 Toast.makeText(getApplicationContext(), "接口请求错误!", Toast.LENGTH_LONG).show();
  142. 142 }
  143. 143 });
  144. 144
  145. 145 }
  146. 146
  147. 147 @Override
  148. 148 public void onClick(View view) {
  149. 149 switch (view.getId()) {
  150. 150 case R.id.btn_get: //使用原生的okhttp请求网络数据
  151. 151 getByOkGo();//用okgo方法获取数据
  152. 152 break;
  153. 153 case R.id.btn_resolvebyfastjson:
  154. 154 jsonToJavaListByFastJson();
  155. 155 break;
  156. 156 }
  157. 157 }
  158. 158
  159. 159
  160. 160 }

 布局文件

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. 3 android:orientation="vertical" android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent">
  5. 5
  6. 6 <Button
  7. 7 android:id="@+id/btn_get"
  8. 8 android:layout_width="match_parent"
  9. 9 android:layout_height="wrap_content"
  10. 10 android:text="get方法获取数据"
  11. 11 android:textAllCaps="false"/>
  12. 12
  13. 13 <Button
  14. 14 android:id="@+id/btn_resolvebyfastjson"
  15. 15 android:layout_width="match_parent"
  16. 16 android:layout_height="wrap_content"
  17. 17 android:text="通过fastjson解析"
  18. 18 android:textAllCaps="false"/>
  19. 19
  20. 20 <TextView
  21. 21 android:id="@+id/tv_result"
  22. 22 android:layout_width="match_parent"
  23. 23 android:layout_height="wrap_content"/>
  24. 24
  25. 25
  26. 26 <TextView
  27. 27 android:id="@+id/tv_wendu"
  28. 28 android:layout_width="match_parent"
  29. 29 android:layout_height="wrap_content"/>
  30. 30
  31. 31 <TextView
  32. 32 android:id="@+id/tv_do"
  33. 33 android:layout_width="match_parent"
  34. 34 android:layout_height="wrap_content"/>
  35. 35
  36. 36 <TextView
  37. 37 android:id="@+id/tv_ph"
  38. 38 android:layout_width="match_parent"
  39. 39 android:layout_height="wrap_content"/>
  40. 40
  41. 41 <TextView
  42. 42 android:id="@+id/tv_tds"
  43. 43 android:layout_width="match_parent"
  44. 44 android:layout_height="wrap_content"/>
  45. 45
  46. 46 <TextView
  47. 47 android:id="@+id/tv_zhuodu"
  48. 48 android:layout_width="match_parent"
  49. 49 android:layout_height="wrap_content"/>
  50. 50
  51. 51 </LinearLayout>

最后的效果(图2)

 

 

           (图2)

       

原文链接:http://www.cnblogs.com/maritimeclimate/p/14440230.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号