【问题标题】:Get a key value from the JSON response of JAVA rest api call从 JAVA rest api 调用的 JSON 响应中获取一个键值
【发布时间】:2021-10-15 06:18:43
【问题描述】:

我有休息电话,我想从 JSON 响应中获取关键值之一,例如“employeeID”的值。

这是其余调用的代码:



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;



public class App {

public static String username = "Test_Username";

    public static void main(String[] args) {
        

        try {
            URL url = new URL ("http://api.com/" + username);
            String encoding = Base64.getEncoder().encodeToString(":".getBytes("utf-8"));

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setRequestProperty  ("Authorization", "Basic " + encoding);
            InputStream content = (InputStream)connection.getInputStream();
            BufferedReader in   = 
                new BufferedReader (new InputStreamReader (content));   
         String line = "";
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch(Exception e) {
           e.printStackTrace();
        }
    }
}

这是 JSON 响应的示例:

[{
    "user": {
        "userID": "Adam555",
        "name": "Test Account",
        "id": 1055287,
        "recentApps": null,
        "employeeID": "2991KDS"
    },
    "user_state": {
        "EmployeeLookUpDBId": 1055304,
        "userID": "Adam555",
        "username": "Adam555"}]

那么如何在 System.out.println 中获取它,例如说“2991KDS”?

【问题讨论】:

  • @crocarneiro 不幸的是,不,我只想获得一个值,而不是整个 JSON 响应。我已经成功获得了响应,我只想从响应中提取一个值。
  • @AdamAyman 解析 JSON 响应后,很容易从 JSON 响应中提取任何值。请在 Google 上搜索“如何在 Java 中使用 JSON 响应”,而不是在 StackOverflow 上发布。

标签: java json


【解决方案1】:

将此库导入您的 pom.xml 文件

`<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>
`

`
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import org.json.JSONObject;
import org.json.JSONTokener;

public class App {

  public static String username = "Test_Username";

  public static void main(String[] args) {


    try {
      URL url = new URL ("http://api.com/" + username );
      String encoding = Base64.getEncoder().encodeToString(":".getBytes("utf-8"));

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");
      connection.setDoOutput(true);
      connection.setRequestProperty  ("Authorization", "Basic " + encoding);
      InputStream content = (InputStream)connection.getInputStream();
      JSONTokener tokener = new JSONTokener(content);
      JSONObject json = new JSONObject(tokener);

        System.out.println(json.getJSONObject("user").get("employeeID"));

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
`

【讨论】:

  • 非常感谢它已经接近了,我现在只有一个小问题它抛出一个错误,因为 JSON 响应以 [{ 我试​​图用 "" 替换 [ 但到目前为止没有运气。 @Chabo
  • @AdamAyman 你的 JSON 结构是什么?因为这将决定我们如何访问这些字段。
  • @AdamAyman 检查我的更新。 { "user": { "userID": "Adam555", "name": "Test Account", "id": 1055287, "recentApps": null, "employeeID": "2991KDS" }, "user_state": { "EmployeeLookUpDBId": 1055304, "userID": "Adam555", "username": "Adam555"} }
  • 这是我的结构,它以 [ 开头,导致问题[{ "user": { "userID": "Adam555", "name": "Test Account", "id": 1055287, "recentApps": null, "employeeID": "2991KDS" }, "user_state": { "EmployeeLookUpDBId": 1055304, "userID": "Adam555", "username": "Adam555"}]
  • @AdamAyman 这不是有效的 JSON 格式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2017-01-21
  • 2017-06-01
  • 1970-01-01
  • 2022-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多