如何解决START_ARRAY令牌错误

时间:2014-11-23 20:01:04

标签: java android json parsing

有谁可以说我做错了。我有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。请有人告诉我什么是错的。

因为我尝试了很多东西,但我找不到解决办法。

我该如何解决?

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

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