如何修复“Out of START_ARRAY token Error”错误

3
有人能说一下我错在哪里吗?我有这样的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);

最后我得到了“START_ARRAY token”错误。请问有人知道是什么问题吗?
因为我已经尝试了很多方法,但是我从未找到解决方案。
请问该如何修复这个错误?

您的字符串不是有效的 JSON! - Mohammad Rahchamani
服务器的API刚刚给了我这个。我只是更改了一些虚拟数据。那么我该如何修复JSON呢@MohammadRahchamani - dummyDroid
看我的回答,这里有一个例子给你! - Mohammad Rahchamani
你可能遇到了解析数据为JSONObject的问题,因为它是一个JSONArray(以[开头,以]结尾)。 - joao2fast4u
2个回答

2

你的json字符串是正确的,但不适用于你期望的对象,正如其他人已经提到的,你需要使用List。

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("");
        }
    }


}

Shards.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;
    }
}

我像这样进行了更改,但是结果还是一样。没有任何变化。我收到了相同的错误。message = "{"shards":" + message + "}";TypeReference<List<Shards>> typeRef = new TypeReference<List<Shards>>() { }; ObjectMapper mapper = new ObjectMapper(); List<Shards> list = mapper.readValue(message, typeRef); - dummyDroid
@dummyDroid 我已经编辑了我的原始回复,并添加了该类的完整源代码。我能够解析原始的json字符串,然后是更新的ShardsResponse json字符串。由于jackson需要,我确实不得不为ShardsResponse添加一个无参构造函数。 - Gary Bak
我仍然遇到相同的错误。在这一行:ShardsResponse sr2 = mapper.readValue(srString, typeRef2);我无法通过这一行。我的ShardsResponse类有问题吗? 在上面的ShardsResponse类中添加默认构造函数以供您的示例使用。 - dummyDroid
@dummyDroid 我已经将另外两个类添加到我的响应中,我是根据 JSON 数据手动创建的,没有复制你的。它们可能存在细微差别。 - Gary Bak

0

你有一个jsonArray,但你正在尝试解析一个jsonObject。将你的方法更改为返回对象列表而不是一个对象。


我把我的代码改成了这样,message = "{"data" : " + message + "}";但是我仍然得到相同的错误。顺便说一下,另一个JSON - 在第一个位置 - 已经是有效的。我在http://json.parser.online.fr/上检查了两个JSON,它们都是有效的。 - dummyDroid
哦!我的错!非常抱歉我的错误!请看我的更新答案! :) - Mohammad Rahchamani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接