【发布时间】:2011-12-05 15:13:51
【问题描述】:
我有一个 JSON 字符串:
"[{\"is_translator\":false,\"follow_request_sent\":false,\"statuses_count\":1058}]"
在字符串上使用 PHP 的 json_decode() 并执行 print_r,输出:
Array
(
[0] => stdClass Object
(
[is_translator] =>
[follow_request_sent] =>
[statuses_count] => 1058
)
)
这表明它是有效的 JSON。
但是使用 Jackson 库会出错:
线程“main”中的异常 org.codehaus.jackson.map.JsonMappingException:无法反序列化 START_ARRAY 令牌中的 java.util.LinkedHashMap 实例 [来源:java.io.StringReader@a761fe;行:1,列:1]
这是简单的代码:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class tests {
public static void main(String [] args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> fwers = mapper.readValue("[{\"is_translator\":false,\"follow_request_sent\":false,\"statuses_count\":1058}]", new TypeReference <Map<String, Object>>() {});
System.out.println(fwers.get("statuses_count"));
}
}
谁能告诉我哪里出了问题和解决办法?
谢谢。
【问题讨论】: