【发布时间】:2017-11-28 04:28:19
【问题描述】:
我有这些 JSON 字符串:
{
"Results": {
"output1": {
"type": "table",
"value": {
"ColumnNames": ["userId", "documentId", "Scored Labels", "Scored Probabilities"],
"ColumnTypes": ["String", "String", "Boolean", "Double"],
"Values": [["100213199594809000000", "1Ktol-SWvAh8pnHG2O7HdPrfbEVZWX3Vf2YIPYXA_8gI", "False", "0.375048756599426"], ["103097844766994000000", "1jYsTPJH8gaIiATix9x34Ekcj31ifJMkPNb0RmxnuGxs", "True", "0.753859758377075"]]
}
}
}
}
我只想拥有ColumnNames 和Values。我已经尝试过这样的事情:
Map<String,Object> map = mapper.readValue(filename, Map.class);
String CN = (String) map.get("ColumnNames");
然后我收到以下错误:
Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('A' (code 65)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@64232b15; line: 1, column: 2]`
我只用过几次 JSON。有人可以帮我吗?
对我来说最好的情况是这样的,我在另一种情况下做过:
String uId = (String) attr.get("userId");
有可能吗?
所以现在我已经这样做了:
我是这样尝试的:
public class ClientPOJO {
private String userId;
private String documentId;
public String getuserId() {
return userId;
}
public void setuserId(String userId) {
this.userId = userId;
}
public String getdocumentId() {
return documentId;
}
public void setdocumentId(String documentId) {
this.documentId = documentId;
}
}
然后:
ObjectMapper mapper = new ObjectMapper();
ClientPOJO clientes= mapper.readValue(filename, ClientPOJO.class);
String uid = clientes.getuserId();
但是现在当我制作一个 Prtinout 时,我会得到和以前一样的错误:
Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('A' (code 65)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@7a6eb29d; line: 1, column: 2]
【问题讨论】:
-
必须使用jackson还是可以使用其他JSON库?
-
其他库也适合我。要点是,我必须以单独的形式获取 userId、documentID 和评分标签。我需要这个,因为我稍后再发送。
-
在这种情况下,请查看其他问题,例如 stackoverflow.com/questions/11874919/…。您需要做的就是获取由“Results”键持有的 json 对象,然后从该对象获取由“value”键持有的对象。从那里获取“ColumnNames”和“Values”的 json 数组。
-
尝试转换为 java 模型对象(POJO),以便访问属性。
-
当我在 Pshemo 的链接中尝试它时,我得到了错误:方法 getJSONObject(int) is undefined for the type JSONArray