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字符串。 java
```android
{ "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" }json
```api
(圖1)數組
執行過程是,當獲取到data JSON對象時,獲取count字符串,這是OneNET平臺給我返回的數值,功能時告訴我datastreams中一共有多少個元素,因此用遍歷datastreams數組,進入datastreams後,獲取id號,id號是用單片機根據NB-IoT文檔(OneNET官網文檔)上傳到雲平臺的,根據這一id號來確認究竟是哪個傳感器。總之何時用getJSONObject或getJSONaRRAY必定要看本身返回的數據是什麼,是大括號仍是中括號,若是都不是隻是一個普通的字符串就用getString方法獲取字符串,getString("value")中""寫的是元素名,拿的是元素值,好比"value":0這一行,拿的就是0這個元素值。網絡
1 package com.example.helloworld.learnokhttp; 2 3 import android.bluetooth.BluetoothClass; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 import androidx.annotation.Nullable; 11 import androidx.appcompat.app.AppCompatActivity; 12 13 import com.alibaba.fastjson.JSON; 14 import com.alibaba.fastjson.JSONArray; 15 import com.alibaba.fastjson.JSONObject; 16 import com.example.helloworld.R; 17 import com.example.helloworld.learnokhttp.javabean.Data; 18 import com.example.helloworld.learnokhttp.javabean.Datapoints; 19 import com.example.helloworld.learnokhttp.javabean.Datastreams; 20 import com.example.helloworld.learnokhttp.javabean.JsonRootBean; 21 import com.lzy.okgo.OkGo; 22 import com.lzy.okgo.adapter.Call; 23 import com.lzy.okgo.cache.CacheMode; 24 import com.lzy.okgo.callback.StringCallback; 25 import com.lzy.okgo.model.Response; 26 27 import org.w3c.dom.Text; 28 29 import java.util.Iterator; 30 import java.util.List; 31 32 public class OkHttpGoActivity extends AppCompatActivity implements View.OnClickListener { 33 34 private String url = "https://api.heclouds.com/devices/523698851/datapoints";// 35 private Button btn_get,btn_resolvebyfastjson; 36 private TextView tv_result,tv_wendu,tv_do,tv_ph,tv_tds,tv_zhuodu; 37 38 @Override 39 protected void onCreate(@Nullable Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_okhttpgo); 42 43 setTitle("用封裝的方法獲取數據"); 44 45 btn_get = findViewById(R.id.btn_get); 46 btn_resolvebyfastjson = findViewById(R.id.btn_resolvebyfastjson); 47 tv_result = findViewById(R.id.tv_result); 48 tv_wendu = findViewById(R.id.tv_wendu); 49 tv_do = findViewById(R.id.tv_do); 50 tv_ph = findViewById(R.id.tv_ph); 51 tv_tds = findViewById(R.id.tv_tds); 52 tv_zhuodu = findViewById(R.id.tv_zhuodu); 53 btn_get.setOnClickListener(this); 54 btn_resolvebyfastjson.setOnClickListener(this); 55 } 56 57 @Override 58 protected void onDestroy() { 59 super.onDestroy(); 60 //Activity銷燬時,取消網絡請求 61 OkGo.getInstance().cancelTag(this); 62 } 63 64 /** 65 *先用okhttpgo拿一次數據 66 *@author home 67 *@time 2021/2/24 10:23 68 */ 69 private void getByOkGo() { 70 OkGo.<String>get(url)//文檔說的第一行泛型必定要添加是指這裏 71 .headers("api-key", "4VdbaFeRQZRwaSTWNhWxb2UEHaw=")//設備的api-key 72 .headers("Content-Type","application/json") 73 .tag(this) 74 .execute(new StringCallback() { 75 @Override 76 public void onSuccess(Response<String> response) {//請求成功回調onSuccess方法 77 tv_result.setText(response.body());//文檔中有說明,body是返回的數據 78 } 79 80 @Override 81 public void onError(Response<String> response) { 82 Toast.makeText(getApplicationContext(), "接口請求錯誤!", Toast.LENGTH_LONG).show(); 83 } 84 }); 85 } 86 87 /** 88 *OkHttpGo與FastJson結合,並顯示到界面 89 *@author home 90 *@time 2021/2/24 10:25 91 */ 92 private void jsonToJavaListByFastJson() { 93 OkGo.<String>get(url)//文檔說的第一行泛型必定要添加是指這裏 94 .headers("api-key", "4VdbaFeRQZRwaSTWNhWxb2UEHaw=")//設備的api-key 95 .headers("Content-Type","application/json") 96 .tag(this) 97 .execute(new StringCallback() { 98 @Override 99 public void onSuccess(Response<String> response) {//請求成功回調onSuccess方法 100 101 String value = "", id = ""; 102 int count = 0; 103 104 JSONObject jsonObjectData = JSONObject.parseObject(response.body()).getJSONObject("data");//獲取json對象後再獲取json對象,即第二層data 105 count = jsonObjectData.getIntValue("count");//count是Datastreams數組的下標值 106 JSONArray jsonArrayDatastreams = jsonObjectData.getJSONArray("datastreams");//獲取json數組即datastearms 107 108 for(int j = 0; j < count; j++) {//遍歷Datastreams數組 109 JSONObject jsonObjectIndex = jsonArrayDatastreams.getJSONObject(j); 110 id = jsonObjectIndex.getString("id"); 111 JSONArray jsonArrayDatapoints = jsonObjectIndex.getJSONArray("datapoints"); 112 JSONObject jsonObjectValue = jsonArrayDatapoints.getJSONObject(0);//Datapoints數組只有一個元素(對象),因此下標是1 113 value = jsonObjectValue.getString("value"); 114 switch (id) { 115 case "3303_0_5700": //溫度 116 //System.out.println("溫度" + value + id); 117 tv_wendu.setText("溫度" + value + "\t設備號" + id); 118 break; 119 case "3300_0_5700": //溶解氧 120 //System.out.println("DO" + value + id); 121 tv_do.setText("溶解氧" + value + "\t設備號" + id); 122 break; 123 case "3327_0_5700": //電導率 124 //System.out.println("電導率" + value + id); 125 tv_tds.setText("TDS" + value + "\t設備號" + id); 126 break; 127 case "3326_0_5700": //ph 128 //System.out.println("ph" + value + id); 129 tv_ph.setText("PH" + value + "\t設備號" + id); 130 break; 131 case "3300_1_5700": //濁度 132 //System.out.println("濁度" + value + id); 133 tv_zhuodu.setText("濁度" + value + "\t設備號" + id); 134 break; 135 } 136 } 137 } 138 139 @Override 140 public void onError(Response<String> response) { 141 Toast.makeText(getApplicationContext(), "接口請求錯誤!", Toast.LENGTH_LONG).show(); 142 } 143 }); 144 145 } 146 147 @Override 148 public void onClick(View view) { 149 switch (view.getId()) { 150 case R.id.btn_get: //使用原生的okhttp請求網絡數據 151 getByOkGo();//用okgo方法獲取數據 152 break; 153 case R.id.btn_resolvebyfastjson: 154 jsonToJavaListByFastJson(); 155 break; 156 } 157 } 158 159 160 }
佈局文件app
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <Button 7 android:id="@+id/btn_get" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="get方法獲取數據" 11 android:textAllCaps="false"/> 12 13 <Button 14 android:id="@+id/btn_resolvebyfastjson" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="經過fastjson解析" 18 android:textAllCaps="false"/> 19 20 <TextView 21 android:id="@+id/tv_result" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content"/> 24 25 26 <TextView 27 android:id="@+id/tv_wendu" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content"/> 30 31 <TextView 32 android:id="@+id/tv_do" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content"/> 35 36 <TextView 37 android:id="@+id/tv_ph" 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content"/> 40 41 <TextView 42 android:id="@+id/tv_tds" 43 android:layout_width="match_parent" 44 android:layout_height="wrap_content"/> 45 46 <TextView 47 android:id="@+id/tv_zhuodu" 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content"/> 50 51 </LinearLayout>
最後的效果(圖2)dom
(圖2)ide