gpt4 book ai didi

java - 如何修复超出 START_ARRAY token 错误

转载 作者:太空狗 更新时间:2023-10-29 13:22:15 25 4
gpt4 key购买 nike

谁能说我哪里做错了。我有这样的 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;
}
}

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 字符串是正确的,但不是您期望的对象,正如有人已经提到的,您需要使用列表

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;


public class ParseJson {

private static final String jsonString = "[{\"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\"}]";

public static void parse() {

try {

TypeReference<List<Shards>> typeRef = new TypeReference<List<Shards>>() { };
ObjectMapper mapper = new ObjectMapper();
List<Shards> list = mapper.readValue(jsonString, typeRef);

for ( Shards s : list )
{
s.printDebug();
}

ShardsResponse sr = new ShardsResponse(list);
String srString = mapper.writeValueAsString(sr);

System.out.println("srString: " + srString );

TypeReference<ShardsResponse> typeRef2 = new TypeReference<ShardsResponse>() { };
ShardsResponse sr2 = mapper.readValue(srString, typeRef2);

sr2.printDebug();


} catch ( IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ParseJson.parse();
}

}

编辑:如果您期望 ShardsResponse 返回,您的 json 字符串应如下所示:

{"shards":[{"locales":["foo3"],"name":"foo","hostname":"foo4","slug":"foo2","region_tag":"foo5"},{"locales":["foo3"],"name":"foo","hostname":"foo4","slug":"foo2","region_tag":"foo5"},{"locales":["foo3"],"name":"foo","hostname":"foo4","slug":"foo2","region_tag":"foo5"},{"locales":["foo3"],"name":"foo","hostname":"foo4","slug":"foo2","region_tag":"foo5"}]}

弄清楚 json 是什么样子的最简单方法是将其转储出来:

ShardsResponse sr = new ShardsResponse(list);
String srString = mapper.writeValueAsString(sr);
System.out.println("srString: " + srString );

编辑:

为清楚起见添加额外的类:

ShardsResponses.java

import java.util.ArrayList;
import java.util.List;


public class ShardsResponse {

private List<Shards> shards = new ArrayList<Shards>();

public ShardsResponse() { }

public ShardsResponse( List<Shards> shards)
{
this.shards = shards;
}

public List<Shards> getShards() {
return shards;
}

public void setShards(List<Shards> shards) {
this.shards = shards;
}

public void printDebug()
{
for ( Shards s : shards)
{
s.printDebug();
System.out.println("");
}
}


}

fragment .java:

import java.util.List;


public class Shards {

private List<String> locales;
private String name;
private String hostname;
private String slug;
private String region_tag;


public List<String> getLocales() {
return locales;
}
public void setLocales(List<String> locales) {
this.locales = locales;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public void printDebug()
{
System.out.println("name: " + name);
System.out.println("hostname: " + hostname);
System.out.println("slug: " + slug);
System.out.println("region_tag: " + region_tag);
for ( String s : locales )
{
System.out.println("Locals: " + locales);
}
}
public String getRegion_tag() {
return region_tag;
}
public void setRegion_tag(String region_tag) {
this.region_tag = region_tag;
}
}

关于java - 如何修复超出 START_ARRAY token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com