【发布时间】:2014-11-23 20:01:04
【问题描述】:
谁能说我哪里做错了。我有这样的json
[{"name":"foo","slug":"foo2","locales":["foo3"],"hostname":"foo4","region_tag":"foo5"},{"name":"foo","slug":"foo2","locales":["foo3"],"hostname":"foo4","region_tag":"foo5"},{"name":"foo","slug":"foo2","locales":["foo3"],"hostname":"foo4","region_tag":"foo5"},{"name":"foo","slug":"foo2","locales":["foo3"],"hostname":"foo4","region_tag":"foo5"}]
我解析到这个类。
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder({
"shards"
})
public class ShardsResponse extends Response{
@JsonProperty("shards")
private List<Shards> shards = new ArrayList<Shards>();
/**
*
* @return
* The shards
*/
@JsonProperty("shards")
public List<Shards> getShards() {
return shards;
}
/**
*
* @param shards
* The shards
*/
@JsonProperty("shards")
public void setShards(List<Shards> shards) {
this.shards = shards;
}
}
碎片类是:
/**
*
* @return
* The locales
*/
@JsonProperty("locales")
public List<String> getLocales() {
return locales;
}
/**
*
* @param locales
* The locales
*/
@JsonProperty("locales")
public void setLocales(List<String> locales) {
this.locales = locales;
}
/**
*
* @return
* The name
*/
@JsonProperty("name")
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The hostname
*/
@JsonProperty("hostname")
public String getHostname() {
return hostname;
}
/**
*
* @param hostname
* The hostname
*/
@JsonProperty("hostname")
public void setHostname(String hostname) {
this.hostname = hostname;
}
/**
*
* @return
* The slug
*/
@JsonProperty("slug")
public String getSlug() {
return slug;
}
/**
*
* @param slug
* The slug
*/
@JsonProperty("slug")
public void setSlug(String slug) {
this.slug = slug;
}
}
所以我使用ObjectMapper.readValue(jsontext, responseclass)
JSONObject object = new JSONObject(JsonString);
JsonString = "";
Iterator<String> keys= object.keys();
while (keys.hasNext()){
String keyValue = (String)keys.next();
JsonString= JsonString+ object.getString(keyValue);
}
JsonString= JsonString.substring(1, JsonString.length()-1);
Object response = ObjectMapper.readValue(JsonString, ShardsResponse.class);
最后我得到了out of START_ARRAY token。请任何人告诉我有什么问题。
因为我尝试了很多事情,但我始终找不到解决方案。
我该如何解决。
【问题讨论】:
-
您的字符串不是有效的 json!
-
服务器的api就是这样给我的。我只是将其更改为虚拟数据。那么我该如何修复json然后@MohammadRahchamani
-
看我的回答,给你一个例子!
-
您必须在将数据解析为 JSONObject 时遇到问题,因为它是 JSONArray(以 [ 开头并以 ] 结尾)。