【问题标题】:Problems Merging JSON Data -- ObjectMapper (HashMap)合并 JSON 数据的问题——ObjectMapper (HashMap)
【发布时间】:2015-09-04 16:16:37
【问题描述】:

我有一个包含字典的 JSON 文件。对于每个英语单词,都有一个与之关联的对象。每个对象都是标签和键的列表。一些示例数据:

}, 
"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 我在问题末尾添加了一个示例。我希望这会有所帮助!

标签: java json hashmap jackson


【解决方案1】:

这是罪魁祸首:

//get whats actually inside this word
Map<String, Integer> tags = (Map<String, Integer>) inDict.get(entry);

应该是:

Map<String, Integer> tags = (Map<String, Integer>) entry.getValue();

【讨论】:

  • 至少修复了部分问题!现在它给了我,JSON 中不允许的 Map 的 Null 键(使用转换 NullKeySerializer?)
  • 我猜这意味着也应该计算一种空后缀?意思是我需要用一些指定的键值替换 null?
  • 确实,JSON 对象中不能有空键(尽管在 Javascript 中是可能的)。您需要测试该值,并且如您所说,使用特殊标签。
  • 或者干脆跳过空后缀:)
  • 我以为这个错误会在这里: if(stemKey.equals(null)) stemKey = "null_value"; ...但不是吗?
猜你喜欢
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2010-11-03
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多