解析包装在双精度数组中的Merriam Webster JSON数据属性?

时间:2018-12-01 06:09:09

标签: java android json parsing

我正在尝试使用Merriam Webster API-https://dictionaryapi.com/products/json#sec-2.def

的文档来获取单词的定义

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

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

我的问题是能够访问“有感觉”的JSONObject,因为我当前在数组中,所以无法访问有意义的对象。我尝试在代码后添加.getJSONArray(0)和.getJSONObject(0),而这两种解决方案均无效。

如何继续将当前需要的JSON解析为“ dt”字符串?

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

1 个答案:

答案 0 :(得分:1)

在这里。

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