【问题标题】:How to fix Out of START_ARRAY token Error如何修复超出 START_ARRAY 令牌错误
【发布时间】: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(以 [ 开头并以 ] 结尾)。

标签: java android json parsing


【解决方案1】:

您的 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> typeRef = new TypeReference>() { }; ObjectMapper 映射器 = 新 ObjectMapper(); List list = mapper.readValue(message, typeRef);
  • @dummyDroid 我已经编辑了我的原始回复并添加了课程的完整来源。我能够解析原始 json,然后解析较新的 ShardsResponse json 字符串。我确实必须为 ShardsResponse 添加一个无参数构造函数,因为 jackson 需要它。
  • 我仍然遇到同样的错误。在这一行: ShardsResponse sr2 = mapper.readValue(srString, typeRef2);我不能通过这条线。我的 ShardsResponse 类有错吗?向上的 ShardsResponse 类 + 为您的示例添加默认构造函数。
  • @dummyDroid 我已将其他两个类添加到我的响应中,我根据 json 数据手动创建了这些,我没有复制你的。它们之间可能存在细微差别。
【解决方案2】:

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

【讨论】:

  • 我这样修改了我的代码,message = "{\"data\" : " + message + "}";但我仍然得到同样的错误。顺便说一句,另一个 json - 首先 - 已经有效。我在json.parser.online.fr上检查了两个json,都是有效的。
  • 哦!我的错!我很抱歉我的错误!看我更新的答案! :)
猜你喜欢
  • 2021-09-17
  • 2021-12-19
  • 1970-01-01
  • 2020-07-14
  • 2014-04-16
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多