我需要将从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]
}
}
"]
发布于 2021-01-13 13:26:17
将响应作为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());https://stackoverflow.com/questions/65693424
复制相似问题