簡體   English   中英

Android,在 Java 結構中解析 JSON

[英]Android, parse JSON in a Java structure

我需要將我從 HTTP 服務器收到的 JSON 轉換為 JAVA 結構,該結構允許我檢索每個數據,例如freq列表的第一個元素。 我能怎么做? 對於 HTTP 請求,我使用 OkHttpClient。

 [" { "cpu": { "freq": [3193.068625, 1600.0, 3900.0], "usage": [0.0, 0.0, 2.0, 1.0, 4.9, 2.0, 1.0, 1.0], "load": [0.22, 0.18, 0.16] }, "memory": { "ram": [15.54, 13.07, 15.9, 2.04, 11.24] }, "disks": { "ssd": [219.1, 13.6, 194.2, 6.6], "hdd": [0.0, 0.0, 0, 100.0] }, "temps": { "core-0": [29.0, 85.0, 105.0], "core-1": [36.0, 85.0, 105.0], "core-2": [35.0, 85.0, 105.0], "core-3": [33.0, 85.0, 105.0] } } "]

將響應作為 JSONObject 並知道響應的格式,您可以使用以下命令:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

// ...
   

JSONObject opt = response.getJSONObject("opt");
JSONObject cpu = opt.getJSONObject("cpu");
JSONArray jsonArrayfreq = cpu.getJSONArray("freq");    

// Finally, charge the JSONArray into your own list and process it.
List<Double> myList = new Gson().fromJson(jsonArrayfreq.toString(), new TypeToken<List<Double>>(){}.getType());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM