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 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 one rror(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 valuehttps://www.cnblogs.com/maritimeclimate/p/= "", id = ""; 102 int count = 0; 103 104 JSONObject jsonObjectData = https://www.cnblogs.com/maritimeclimate/p/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 = https://www.cnblogs.com/maritimeclimate/p/jsonArrayDatapoints.getJSONObject(0);//Datapoints陣列只有一個元素(物件),所以下標是1 113 value = https://www.cnblogs.com/maritimeclimate/p/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 one rror(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 }
布局檔案
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)

(圖2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/263302.html
標籤:Android
上一篇:dependencies的問題
