首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android,在Java结构中解析JSON

Android,在Java结构中解析JSON
EN

Stack Overflow用户
提问于 2021-01-13 07:12:46
回答 1查看 72关注 0票数 0

我需要将从HTTP服务器收到的JSON转换为允许我检索每一个数据的JAVA结构,例如freq列表的第一个元素。我该怎么做呢?对于HTTP请求,我使用OkHttpClient。

代码语言:javascript
复制
["
  {
    "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]
      } 
   }
"]

EN

回答 1

Stack Overflow用户

发布于 2021-01-13 13:26:17

将响应作为JSONObject,并知道响应的格式,您可以使用以下命令:

代码语言:javascript
复制
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());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65693424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档