首页 > 解决方案 > 如何在java中解码这个json对象数据

问题描述

我得到了这个 json 对象数据,它是

{
  "data": {
    "id": 2,
    "email": "janet.weaver@reqres.in",
    "first_name": "Janet",
    "last_name": "Weaver",
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
  },
  "ad": {
    "company": "StatusCode Weekly",
    "url": "http://statuscode.org/",
    "text": "A weekly newsletter  focusing on software development, infrastructure, the server, performance, and the stack end of things."
  }
}

我想解析我想在对象中打印输出电子邮件的 json...我使用 org.simple.json 库。

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("curl -s -S https://reqres.in/api/users/2");

BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String result = br.readLine();
Object obj=JSONValue.parse(result);

如何通过数据打印电子邮件数据 -> 电子邮件

标签: javajson

解决方案


您可以将值转换为JSONObject并进一步使用JSONObjectAPI 进行打印

    JSONObject jsonObject = (JSONObject) JSONValue.parse(result);
    JSONObject data = (JSONObject) jsonObject.get("data");
    String email= (String) data.get("email");
    System.out.println("Email= " + email);

推荐阅读