我有一個包含字典的JSON文件。對於每個英文單詞都有一個與之相關的對象。每個對象都是標籤和鍵的列表。一些樣本數據:合併JSON數據的問題 - ObjectMapper(HashMap)
},
"On": {
"ADP": 345,
"PRT": 1
},
"On-to-Spokane": {
"ADJ": 1
},
"Once": {
"ADP": 28,
"ADV": 57
},
"One": {
"NOUN": 76,
"NUM": 343
},
我想創建一個新的JSON文件在我去的每個字在已有的詞集,並取詞的結尾。如果結尾的單詞已經存在於新地圖中,並且已經存在於新地圖中,則該單詞中的數據需要添加到新地圖中。
InvertedStemmerDB data = new InvertedStemmerDB();
inDict = mapper.readValue(new File(dictionary), Map.class);
//Hold Stem Data... creating a whole new look-up dictionary for word endings
Map<String,Object> stemData = new HashMap<String,Object>();
//Open up existing data
ObjectMapper mapper = new ObjectMapper();
//Map<String,Object> data1 = mapper.readValue(new File("*****.json"), Map.class);
//Go through All the Data
for (Map.Entry<String, Object> entry : inDict.entrySet()) {
//this is how I'm stripping the word.. this isn't the issue
String key = entry.getKey();
stemmer.setCurrent(key);
stemmer.stem();
String stemKey = stemmer.getCurrent();
String suffix = suffix(key, stemKey);
//get whats actually inside this word
Map<String, Integer> tags = (Map<String, Integer>) inDict.get(entry);
if (stemData.containsKey(suffix)) {//This means it needs to be updated, the stem already exists
//get object sub-value
Map<String, Integer> stemTag = (Map<String, Integer>) stemData.get(suffix);
double value = 0;
for (String tag : tags.keySet()) {
//add to stemData
if(stemTag.containsKey(tag)){
int previousValue = stemTag.get(tag);
int resultantValue = tags.get(tag);
stemTag.put(tag, resultantValue+previousValue);
}
else
stemTag.put(tag, tags.get(tag));
}
}
else {
//needs to be created, not updated
stemData.put(suffix, inDict.get(key));
}
}
mapper.writeValue(new File("test_write.json"), stemData);
後 '固定' 的幾個問題,我「米仍然得到一個NullPointerException ..
輸出示例:
因此,對於樣本數據:
"running": {
"ADJ": 1
},
"dancing": {
"ADP": 28,
"ADV": 57
},
"hopeing": {
"ADJ":14
},
輸出成爲:
"ing": {
"ADJ": 15
"ADP": 28
"ADV": 57
請張貼在JSON樣本輸出數據要生成。 –
@NikolayRusev我在問題的最後添加了一個例子。我希望這有幫助! –