1

我有一个简单的杰克逊解析器,它应该返回我的值,但我只得到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 ?
4

1 回答 1

5

我的 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());
}
于 2013-11-12T23:25:43.123 回答