I've tried searching this topic for two days now without coming across an answer. For this topic, it feels like searching for a needle in a haystack.
I'm trying to parse a JSONArray so I can put the values into a String[], so I can have a map of words and their definitions.
I'm using Java and json-simple on IntelliJ.
My JSON file has the following format:
{
"word 1": ["Definition 1.", "Definition 2.", "Definition 3."],
"word 2": ["Definition includes \"quotes\"."]
}
I can get the word no problem but the definition is causing trouble. So far all I've been able to get is the raw definition String as it appears above.
For example:
word 2
["Definition includes \"quotes\"."]
I found one resource that seemed to have a similar JSON structure. I also saw this post. I came up with the following test based on those examples.
private void loadJSONDictionary() {
JSONObject jsonDictionary = null;
JSONParser parser = new JSONParser();
try (FileReader reader = new FileReader(this.fileName)) {
jsonDictionary = (JSONObject) parser.parse(reader);
for (Object key : jsonDictionary.keySet()) {
String word = key.toString();
JSONArray definitions = (JSONArray) jsonDictionary.get(key);
for (Object definition : definitions) {
System.out.println(definition.toString());
}
}
}
// catch clauses
}
This gives me the exception: class java.lang.String cannot be cast to class org.json.simple.JSONArray
when I try to cast to JSONArray
.
I also tried creating a new JSONArray()
and adding the definitions to that which just gives me null
.
From reading other posts, it seems like I'm close but no matter what I try, I either get an exception or null
.
Is there a way to get the definitions for each word into a String[] through parsing? Obviously I'm new to this. I'm open to using other APIs if that helps.
This library may be out of date and has bugs, you can create some breakpoints to debug the final jsonDictionary. To me, Gson or FastJson are used more widely. Below is a working sample for your code in Gson:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class JsonTest {
public static void main(String[] args) {
String jsonString = "{\n" +
" \"word 1\": [\"Definition 1.\", \"Definition 2.\", \"Definition 3.\"], \n" +
" \"word 2\": [\"Definition includes \\\"quotes\\\".\"]\n" +
"}";
Type empMapType = new TypeToken<Map<String, List<String>>>() {}.getType();
Map<String, List<String>> nameEmployeeMap = new Gson().fromJson(jsonString, empMapType);
for(String key : nameEmployeeMap.keySet()) {
System.out.println("key =" + key);
List<String> value = nameEmployeeMap.get(key);
System.out.println("value =" + value);
for (String v : value) {
System.out.println("v =" + value);
}
}
}
}
The output is:
key =word 1
value =[Definition 1., Definition 2., Definition 3.]
v =[Definition 1., Definition 2., Definition 3.]
v =[Definition 1., Definition 2., Definition 3.]
v =[Definition 1., Definition 2., Definition 3.]
key =word 2
value =[Definition includes "quotes".]
v =[Definition includes "quotes".]
check https://www.baeldung.com/gson-json-to-map for more info.