Jackson ObjectMapper意外的字符 - JSON有效

tom*_*ots 4 java json file jackson

我正在尝试使用Jackson写一个简单的类到一个文件,我创建后无法读取该文件.我明白了

org.codehaus.jackson.JsonParseException:意外的字符('.'(代码46)):在[来源:java]预期有效值(数字,字符串,数组,对象,'真','假'或'空') .io.StringReader @ f2dec59; line:1,column:2]

我的目标很简单; 它基本上只是HashMap的容器.这是我使用JSONLint检出的结果JSON文件:

{
"quaternions": {
    "10": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 1,
        "identity": true
    },
    "11": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 1,
        "identity": true
    },
    "12": {
        "x": 0,
        "y": 0,
        "z": 0,
        "w": 0,
        "identity": false
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我用来读取文件的代码如下:

TypeReference<ZeroQuaternions> typeRef;
typeRef = new TypeReference<ZeroQuaternions>() {};
ZeroQuaternions readQuats = mapper.readValue("./zeroQuatTest.json", typeRef);
Run Code Online (Sandbox Code Playgroud)

小智 5

您有此错误,因为jackson尝试反序列化./zeroQuatTest.json字符串而不是文件的内容.试着打电话

TypeReference<ZeroQuaternions> typeRef;
typeRef = new TypeReference<ZeroQuaternions>() {};
ZeroQuaternions readQuats = mapper.readValue(new File("./zeroQuatTest.json"), typeRef);
Run Code Online (Sandbox Code Playgroud)