I need to load a text file of information into Java. The Text file looks like this
我需要将信息的文本文件加载到Java中。 Text文件看起来像这样
"reproduce": {
"VB": 7
},
"drill": {
"VB": 8,
"NN": 16
},
"subgross": {
"JJ": 2
},
"campsites": {
"NNS-HL": 1,
"NNS": 1
},
"streamed": {
"VBN": 1,
"VBD": 2
}
It is basically a huge collection of words with some tags included. I need to save this information in some sort of Java data-structure so that the program can search and retrieve tag statistics for a given word.
它基本上是一个包含一些标签的大量单词集合。我需要将此信息保存在某种Java数据结构中,以便程序可以搜索和检索给定单词的标记统计信息。
From what I've read, using a type of HashMap would be the best idea? Something like:
根据我的阅读,使用一种HashMap是最好的主意吗?就像是:
Map<KeyType, List<ValueType>>
Is that a good idea? How would I go about scanning this data from the text file? I could probably find a way to print the dictionary to the text file that would be easier to scan into Java.
这是一个好主意吗?我该如何从文本文件中扫描这些数据?我可能会找到一种方法将字典打印到文本文件中,这样可以更容易地扫描到Java中。
1 个解决方案
#1
1
While your input does not look exactly like JSON, you might be able to preprocess[1] it in a simple way to make it valid JSON. Because JSON is probably much more widespread and therefore better supported than your custom format.
虽然您的输入看起来与JSON不完全相同,但您可以通过简单的方式对其进行预处理[1]以使其成为有效的JSON。因为JSON可能更广泛,因此比您的自定义格式更好地支持。
If your problem then is JSON deserialization, then take a look at Jackson or Gson, which will convert your input string into objects.
如果您的问题是JSON反序列化,那么看看Jackson或Gson,它会将您的输入字符串转换为对象。
Simple example in Jackson:
杰克逊的简单例子:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
Map<String,Object> data = mapper.readValue(new File("file.json"), Map.class);
// process data further here ...
Both Jackson and Gson have a lot of options and can handle complex inputs in various ways, e.g. they can serialize and deserialize from and to Maps, custom Objects, can handle polymorphism (mapping different inputs to objects of different classes) and more.
Jackson和Gson都有很多选择,可以通过各种方式处理复杂的输入,例如:他们可以从地图序列化和反序列化,自定义对象,可以处理多态(将不同的输入映射到不同类的对象)等等。
Given the input, that is currently in your question, you can simply prepend and append a curly bracket, and you would have valid JSON:
根据您的问题中的输入,您可以简单地添加和附加一个大括号,并且您将拥有有效的JSON:
{
"reproduce": {
"VB": 7
},
"drill": {
"VB": 8,
"NN": 16
},
"subgross": {
"JJ": 2
},
"campsites": {
"NNS-HL": 1,
"NNS": 1
},
"streamed": {
"VBN": 1,
"VBD": 2
}
}
#1
1
While your input does not look exactly like JSON, you might be able to preprocess[1] it in a simple way to make it valid JSON. Because JSON is probably much more widespread and therefore better supported than your custom format.
虽然您的输入看起来与JSON不完全相同,但您可以通过简单的方式对其进行预处理[1]以使其成为有效的JSON。因为JSON可能更广泛,因此比您的自定义格式更好地支持。
If your problem then is JSON deserialization, then take a look at Jackson or Gson, which will convert your input string into objects.
如果您的问题是JSON反序列化,那么看看Jackson或Gson,它会将您的输入字符串转换为对象。
Simple example in Jackson:
杰克逊的简单例子:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
Map<String,Object> data = mapper.readValue(new File("file.json"), Map.class);
// process data further here ...
Both Jackson and Gson have a lot of options and can handle complex inputs in various ways, e.g. they can serialize and deserialize from and to Maps, custom Objects, can handle polymorphism (mapping different inputs to objects of different classes) and more.
Jackson和Gson都有很多选择,可以通过各种方式处理复杂的输入,例如:他们可以从地图序列化和反序列化,自定义对象,可以处理多态(将不同的输入映射到不同类的对象)等等。
Given the input, that is currently in your question, you can simply prepend and append a curly bracket, and you would have valid JSON:
根据您的问题中的输入,您可以简单地添加和附加一个大括号,并且您将拥有有效的JSON:
{
"reproduce": {
"VB": 7
},
"drill": {
"VB": 8,
"NN": 16
},
"subgross": {
"JJ": 2
},
"campsites": {
"NNS-HL": 1,
"NNS": 1
},
"streamed": {
"VBN": 1,
"VBD": 2
}
}