【问题标题】:Parse using Jackson Parser使用 Jackson Parser 进行解析
【发布时间】:2015-12-08 09:59:36
【问题描述】:

我刚刚开始使用杰克逊图书馆,但我不知道如何解析我的数据。有人可以帮忙吗?

这是我的 json 响应...

{"flag":"1",
"data":[{"categoty_id":"150","category_name":"Baby &Kids"},{"categoty_id":"151","category_name":"Books & Comics"},{"categoty_id":"152","category_name":"Collectibles & Antiques"},{"categoty_id":"153","category_name":"Electronics"} ...and so on

为了解析我正在使用的数据

 try {
                            ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
                            parameters.add(new NameValuePair("store_id", "1"));

                            Log.e("Cat", "start querying...");
                            final String response = GetResponse.execPostScript("category.php?store_id=1");
                            Log.e("Cat",response);

                            JsonFactory jsonFactory = new JsonFactory();
                            JsonParser jsonParser = jsonFactory.createParser(response);
                            allCategory = new ObjectMapper().readValue(jsonParser, AllCategory.class);



                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            pD.dismiss();

                        }

我的 ProJo 课程是生活跟随

package com.elsner.bean;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "flag",
        "data"
})
public class AllCategory {

    @JsonProperty("flag")
    private String flag;
    @JsonProperty("data")
    private List<Category> data = new ArrayList<Category>();
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     *
     */
    public AllCategory() {
    }

    /**
     *
     * @param flag
     * @param data
     */
    public AllCategory(String flag, List<Category> data) {
        this.flag = flag;
        this.data = data;
    }

    /**
     *
     * @return
     * The flag
     */
    @JsonProperty("flag")
    public String getFlag() {
        return flag;
    }

    /**
     *
     * @param flag
     * The flag
     */
    @JsonProperty("flag")
    public void setFlag(String flag) {
        this.flag = flag;
    }

    /**
     *
     * @return
     * The data
     */
    @JsonProperty("data")
    public List<Category> getData() {
        return data;
    }

    /**
     *
     * @param data
     * The data
     */
    @JsonProperty("data")
    public void setData(List<Category> data) {
        this.data = data;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

我收到以下运行时错误........

com.fasterxml.jackson.databind.JsonMappingException: 无法从 START_ARRAY 令牌中反序列化 com.elsner.bean.AllCategory 的实例

但我在解析数据时遇到错误。

【问题讨论】:

  • 你能发布错误吗?
  • 我收到编译时错误@此代码 AllCategory allCategory = new ObjectMapper().readValues(jsonParser,AllCategory.class);.....thanx 快速重播跨度>
  • 你遇到了什么错误?
  • 好的,我明白了....我使用 readValues() 而不是 readValue()....谢谢大家
  • 好的,我已经解决了这个问题......这是我的错误......我已经将代码放入循环......那不应该......看我编辑的问题......它是解决了

标签: android json jackson


【解决方案1】:

ObjectMapper 类没有readValues() 方法,请改用readValue()

使用这个:

 AllCategory[] allCategory = new ObjectMapper().readValue(jsonParser,AllCategory[].class);

【讨论】:

  • 这可行,但现在我收到运行时错误.....com.fasterxml.jackson.databind.JsonMappingException:无法反序列化 START_ARRAY 令牌中的 com.elsner.bean.AllCategory 实例
  • 我已经改变了我的问题...一旦我解决了我的问题,我会接受你的回答
  • at后面的内容是什么?
  • 您的 json 包含一个数组,但您试图将其解析为对象。出现此错误是因为对象必须从 { 开始。
猜你喜欢
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多