Kaushik
Kaushik

Reputation: 553

How do I check if an element exists or not while JSON parsing?

I have the following json string. Sometimes, there are multiple definition elements and sometimes there is only one. I am new to JSON parsing and I somehow, in a very primitive way, managed to get the value of the first definition element that occurs. However, I am unable to get all values of definitions since there is no resource that I can find online. Do I have to check if second occurrence exists or is there a better way to do it ?

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 snippet to get definition value:

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

Upvotes: 0

Views: 150

Answers (1)

user5156016
user5156016

Reputation:

Everything looks good but you should stop at getJSONArray("senses").

It should be

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

and then you should loop thru elements of that array. Something like this.

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

But before doing that you should notice that your definitions json is not well formed it should be someting like

"definitions":"blablabl blablab"

Since it is not an array;.

Upvotes: 1

Related Questions