JsonParser返回null而不是值

时间:2013-11-12 15:58:43

标签: json scala streaming jackson

我有一个简单的Jackson解析器,它应该返回给我值,但我只得到null个值。任何想法将不胜感激?

示例Json数据:

{"a":"ab","b":"cd","c":"cd","d":"de","e":"ef","f":"fg"}

代码:

var jfactory = new JsonFactory()
var jParser : JsonParser  = jfactory.createJsonParser(new File(outputDir + "/" + "myDic.json"))

while (jParser.nextToken() != JsonToken.END_OBJECT) {
  var k = jParser.getCurrentName();
  jParser.nextToken();
  var v = jParser.getText();
  println(k +"---" + v)
  phoneDict.put(k,v);
  i = i + 1;
  println(phoneDict.size)
  var t = readLine("Dict Done ?")
}

输出:

null---null
1
Dict Done ?
null---null
1
Dict Done ?
null---null
1
Dict Done ?
null---null
1
Dict Done ?

1 个答案:

答案 0 :(得分:4)

我的Java代码看起来像这样,并且工作得很好:

JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = jsonFactory.createParser(json);

//Skip START_OBJECT
jsonParser.nextToken();

while (JsonToken.END_OBJECT != jsonParser.nextToken()) {
    System.out.println(jsonParser.getCurrentName());
    jsonParser.nextToken();
    System.out.println(jsonParser.getText());
}