-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.

1
  • 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. Commented Mar 13, 2015 at 18:37

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.