【问题标题】:How do I check if an element exists or not while JSON parsing?JSON解析时如何检查元素是否存在?
【发布时间】:2016-12-28 20:28:38
【问题描述】:

我有以下 json 字符串。有时,有多个定义元素,有时只有一个。我是 JSON 解析的新手,我以一种非常原始的方式设法获得了出现的第一个定义元素的值。但是,我无法获得定义的所有值,因为我没有可以在网上找到的资源。我是否必须检查是否存在第二次出现或者是否有更好的方法?

JSON:

{
  "metadata": {
    "provider": "Oxford University Press"
  },
  "results": [
    {
      "id": "edifice",
      "language": "en",
      "lexicalEntries": [
        {
          "entries": [
            {
              "etymologies": [
                "late Middle English: via Old French from Latin aedificium, from aedis dwelling + facere make"
              ],
              "grammaticalFeatures": [
                {
                  "text": "Singular",
                  "type": "Number"
                }
              ],
              "senses": [
                {
                  "definitions": [
                    "a large, imposing building."
                  ],
                  "id": "m_en_gb0256160.001",
                  "registers": [
                    "formal"
                  ]
                },
                {
                  "definitions": [
                    "a complex system of beliefs:"
                  ],
                  "examples": [
                    {
                      "text": "the concepts on which the edifice of capitalism was built"
                    }
                  ],
                  "id": "m_en_gb0256160.002",
                  "registers": [
                    "formal"
                  ]
                }
              ]
            }
          ],
          "language": "en",
          "lexicalCategory": "Noun",
          "pronunciations": [
            {
              "audioFile": "http://audio.oxforddictionaries.com/en/mp3/edifice_gb_1.mp3",
              "dialects": [
                "British English"
              ],
              "phoneticNotation": "IPA",
              "phoneticSpelling": "ˈɛdɪfɪs"
            }
          ],
          "text": "edifice"
        }
      ],
      "type": "headword",
      "word": "edifice"
    }
  ]
}

Java sn -p 获取定义值:

String locations = data.getJSONArray("results").getJSONObject(0).getJSONArray("lexicalEntries").getJSONObject(0).getJSONArray("entries").getJSONObject(0).getJSONArray("senses").getJSONObject(0).get("definitions").toString();

【问题讨论】:

    标签: java json org.json


    【解决方案1】:

    一切看起来都不错,但您应该在 getJSONArray("senses") 处停下来。

    应该是

    JSONArray senses= .....getJSONArray("senses");
    

    然后你应该循环遍历该数组的元素。像这样。

    ArrayList<String> definitions = new ArrayList<String>();
    for(int i =0; i < senses.length(); i++){
        definitions.add(senses.getJSONObject(i).optString("definitions","");
    }
    

    但在此之前,您应该注意到您的定义 json 格式不正确,它应该类似于

    "definitions":"blablabl blablab"
    

    因为它不是数组;。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多