gpt4 book ai didi

java - 操作从 postForObject 返回的 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:25 25 4
gpt4 key购买 nike

我想知道是否有一种方法可以操作 RestTemplate 类的方法 postForObject 返回的 JSON。我读到了一个关于类 JSONObject 的老问题,但我注意到它已被 JsonObject 取代,而且我不知道它是否以完全相同的方式工作。这是我的方法:

public void sendEmail() throws FirebaseAuthException {
Body body = new Body(firebase.createCustomToken("username"), true);
RestTemplate t = new RestTemplate();
Object response = t.postForObject(
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=[API_KEY]",
body, Object.class);
}

firebase.createCustomToken("username") method 是我用来为用户返回自定义 token 的方法。 firebase 对象是 FireBaseService 类的对象我在其中编写了使用 FireBase 的所有有用方法。我传递了一个 String 和一个 boolean 作为 POST 方法的主体。一切正常,但我不知道如何获取某个 JSON 字段的值。 response的值是:

{
"idToken": "[ID_TOKEN]",
"refreshToken": "[REFRESH_TOKEN]",
"expiresIn": "3600"
}

我正在执行的 REST API 调用是这样的:https://firebase.google.com/docs/reference/rest/auth#section-verify-custom-token .
如何获取字段idToken的值?

最佳答案

JSONObject json = (JSONObject) response;
String token = json.getString("refreshToken");

关于java - 操作从 postForObject 返回的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com