如何从 API 获取以下数据? [关闭]

Posted

技术标签:

【中文标题】如何从 API 获取以下数据? [关闭]【英文标题】:How to get following data from API? [closed] 【发布时间】:2021-10-02 22:21:38 【问题描述】:

我想获取 word、string1 和 string2。 抱歉我的英语不好,感谢您的回答。

这是我的代码:

Item item = new Item(jsonObject.getString("word"), 
                     jsonObject.getString("string1"), jsonObject.getString("string2"));

这是来自 API 的数据:

["word":"apple","definitions":["string1":"this is string 1",
                                 "string2":"this is string 2""]]

【问题讨论】:

definitions 是一个数组 查看此答案以供参考 - ***.com/questions/68463691/… 不应该是jsonObject.getJsonArray("definitions").get(0).getString("string1")吗? 首先访问“单词”,然后访问“定义”。根据定义,您可以深入了解 string1 和 string2。 这能回答你的问题吗? How to parse JSON in Java 【参考方案1】:

您的 JSON 格式为:

[
   
      "word":"apple",
      "definitions":[
         
            "string1":"this is string 1",
            "string2":"this is string 2"
         
      ]
   
]

从问题看来您对 JSON 不太熟悉,learn more。

但现在你有一个JSON Array,其中包含JSON Object。又是 JSON ArrayJSON Object

所以要回答你的问题,你应该访问如下内容:

请注意,此代码可能存在一些错误,因为它未经测试。您有责任进行相应的修改。

第 1 步

JSON Array 获取JSON Object。您提供了一些代码,所以我将做出一些假设。

Item item = new Item(jsonObject.getString("word"), 
                     jsonObject.getString("string1"), jsonObject.getString("string2"));

jsonObject 已经拥有来自外部数组的对象。如果是这种情况,请转到第 2 步。否则请继续阅读。

String json = "your json here"; // paste your provided json here
JSONArray jsonArray = new JSONArray(json);

// get the object at index 0
JSONObject jsonObject = jsonArray.getJSONObject(0);

// get the "word" value here
String word = jsonObject.getString("word");

第 2 步

现在我们已经提取了密钥wordvalue。让我们提取definitions。继续上一个示例。

// getting the json array for definition
JSONArray definitions = jsonObject.getJSONArray("definitions");

// get the object at index 1
// use loop if you want to get multiple values from this array
JSONObject first = definitions.getJSONObject(0);

// finally get your string values
String string1 = first.getString("string1");
String string2 = first.getString("string2");

完整代码

String json = "your json here"; // paste your provided json here
JSONArray jsonArray = new JSONArray(json);

// get the object at index 0
JSONObject jsonObject = jsonArray.getJSONObject(0);

// get the "word" value here
String word = jsonObject.getString("word");

// getting the json array for definition
JSONArray definitions = jsonObject.getJSONArray("definitions");

// get the object at index 1
// use loop if you want to get multiple values from this array
JSONObject first = definitions.getJSONObject(0);

// finally get your string values
String string1 = first.getString("string1");
String string2 = first.getString("string2");

那就是你必须先了解JSON ArrayJSON Object,然后才能了解原因和方法。

【讨论】:

【参考方案2】:

如果数据是对象,你可以试试

var a = ["word":"apple","definitions":["string1":"this is string 1","string2":"this is string 2"]]

var string1 = a[0]['definitions'][0]["string1"];
console.log('string 1 =>', string1);
var string2 = a[0]['definitions'][0]["string2"];
console.log('string 1 =>', string1);

【讨论】:

【参考方案3】:

如果您知道如何查看服务器响应,那么您可以使用jsonschema2pojo 生成模型,这很酷。所以你永远不会出错。

只需将您的答案复制到那里,您就会发现一切都是那么简单。 从那以后,当我尝试这个工具时,我不再手动执行这样的转换。

如何从服务器获得响应?许多人为此使用 Retrofit 库,您将在其中指定一个转换器(例如 GSON),将答案转换为使用 jsonschema2pojo 创建的对象。

祝你好运!

【讨论】:

以上是关于如何从 API 获取以下数据? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何从其他网站获取数据? [关闭]

如何从 GraphQL API 调用中获取 JSON 对象元素? [关闭]

如何通过 HTML 页面从游戏手柄获取/发送数据 [关闭]

如何从 laravel 5.2 中的网站提取 API? [关闭]

如何从列表中获取随机字符串值 - android [关闭]

如何将所有数据从 html 表获取到控制器 [关闭]