无法将字符串转换为long

时间:2014-09-16 00:09:00

标签: java json

所以,这是我的代码..

 HashMap<Long, Long> trackCount = new HashMap<Long, Long>(); 
 for (Text value: values)
            {
                String[] chunks = value.toString().split(DELIMITER);
                Long trackId = Long.parseLong(chunks[0]); // see i convert it to Long
                Long frequency = Long.parseLong(chunks[1]);
                Long curFreq = 0l;
                if(trackCount.containsKey(trackId)){
                    curFreq = trackCount.get(trackId);
                }
                trackCount.put(trackId, curFreq + frequency); // key is trackId which is long..
            }
            trackFrequency.put("track_id", key);
            trackFrequency.put("track_counts", trackCount);
            context.write(NullWritable.get(), new Text(trackFrequency.toJSONString()));

这实际上是一个hadoop代码..但我看到的最终输出是

     {"track_counts":{"2":52,"3":2,"7":32},"track_id":1}

现在为什么track_counts中的键是关于字符串的?我以为我把它们转换为Long?

1 个答案:

答案 0 :(得分:4)

The name in JSON name-value pairs can only be a String.无论序列化程序是什么,它都会将映射键转换为String,然后再将其写入JSON。