代码之家  ›  专栏  ›  技术社区  ›  IntrACate

使用json-simple解析JSONArray时遇到问题

  •  0
  • IntrACate  · 技术社区  · 1 年前

    我已经试着搜索这个话题两天了,但没有找到答案。对于这个话题,感觉就像大海捞针。

    我正在尝试解析JSONArray,以便将值放入String[]中,这样我就可以得到单词及其定义的映射。

    我在IntelliJ上使用Java和json simple。

    我的JSON文件格式如下:

    {
        "word 1": ["Definition 1.", "Definition 2.", "Definition 3."], 
        "word 2": ["Definition includes \"quotes\"."]
    }
    

    我可以毫无问题地理解这个词,但这个定义造成了麻烦。到目前为止,我所能得到的只是上面出现的原始定义String。

    例如:

    word 2
    ["Definition includes \"quotes\"."]
    

    我找到一个 resource 它似乎具有类似的JSON结构。我也看到了 this 帖子。我根据这些例子提出了以下测试。

    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
    }
    

    这给了我一个例外: class java.lang.String cannot be cast to class org.json.simple.JSONArray 当我试图投 JSONArray .

    我也试着创造一个 new JSONArray() 并将定义添加到刚才给我的定义中 null .

    从阅读其他帖子来看,我似乎很接近,但无论我尝试什么,我要么得到一个例外,要么 无效的 .

    有没有一种方法可以通过解析将每个单词的定义转换为String[]?显然,我对此并不熟悉。如果有帮助,我愿意使用其他API。

    0 回复  |  直到 1 年前
        1
  •  0
  •   Jacky Liu    1 年前

    此库可能已过时并存在错误,您可以创建一些断点来调试最终的jsonDictionary。在我看来,Gson或FastJson的使用更为广泛。下面是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);
                }
            }
        }
    }
    

    输出为:

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

    检查 https://www.baeldung.com/gson-json-to-map 了解更多信息。