-2

I have the following JSON text and would like to get the values "detest" and "dislike".

{
  "noun": {
    "syn": [
      "hatred",
      "emotion"
    ],
    "ant": [
      "love"
    ]
  },
  "verb": {
    "syn": [
      "detest",
      "dislike"
    ],
    "ant": [
      "love"
    ]
  }
}

I've tried parsing it using the org.json library with the following:

JSONArray obj = new JSONObject(String);
JSONArray arr = obj.getJSONArray("syn");

But I can't seem to access the array this way. Any help is greatly appreciated.

user4668503
  • 3
  • 1
  • 1
  • 3
  • 3
    Show your actual code that reproduces the problem and explain how specifically it is not working for you. Parsing JSON in Java is a topic that is covered repeatedly and extensively in numerous places, I would very highly suggest you do a cursory search and look at a few tutorials. – tnw Mar 13 '15 at 18:37

1 Answers1

2

The following is related to the JSON.simple library.


You need a JSONParser instance to get a JSONObject value from your String object.

JSONObject data;
try {
    JSONParser helper = new JSONParser();
    data = (JSONObject)helper.parse(String);
} catch (ParseException parse) {
    // Invalid syntax
    return;
}
// Note that these may throw several exceptions
JSONObject node = (JSONObject)data.get("verb");
JSONArray array = (JSONArray)node.get("syn");
spongebob
  • 8,370
  • 15
  • 50
  • 83