gpt4 book ai didi

java - 解析包裹在双数组中的 Merriam Webster JSON 数据属性?

转载 作者:行者123 更新时间:2023-11-29 23:25:59 25 4
gpt4 key购买 nike

我正在尝试使用 Merriam Webster 的 API 中的文档获取单词的定义 - https://dictionaryapi.com/products/json#sec-2.def

文档显示了来自服务器的 JSON 响应的结构,我已经能够成功地将原始数据解析为一个包含“sense”对象的数组。

代码和解析数据的截图 enter image description here

我的问题是能够访问“sense”JSONObject,因为我目前在一个数组中,所以我无法访问 sense 对象。我尝试在我的代码后添加 .getJSONArray(0) 和 .getJSONObject(0),但这些解决方案均无效。

如何继续将我当前拥有的这个 JSON 解析为“dt”字符串?

                    JSONArray merriamResults = data.getJSONArray("Merriam")
.getJSONObject(0)
.getJSONArray("def")
.getJSONObject(0)
.getJSONArray("sseq")
.getJSONArray(0)
.getJSONArray(0);

最佳答案

在这里。

String json = "{\"def\":[\n" +
" {\n" +
" \"sseq\":[\n" +
" [\n" +
" [\n" +
" \"sense\",\n" +
" {\n" +
" \"dt\":[[\"text\",\"{bc}a {a_link|backward} somersault especially\n" +
" in the air\"]]\n" +
" }\n" +
" ]\n" +
" ]\n" +
" ]\n" +
" }\n" +
"]}";

try {
JSONObject jsonObject = new JSONObject(json);
String dt = jsonObject.getJSONArray("def")
.getJSONObject(0)
.getJSONArray("sseq")
.getJSONArray(0)
.getJSONArray(0)
.getJSONObject(1)
.getJSONArray("dt")
.getJSONArray(0)
.getString(1);
Log.i("jsondata", dt);
} catch (JSONException e) {
e.printStackTrace();
}

我的日志:

 12-02 21:38:12.316 14174-14174/com.example.pemba.sample I/jsondata: {bc}a {a_link|backward} somersault especially
in the air

关于java - 解析包裹在双数组中的 Merriam Webster JSON 数据属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568248/

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