解析使用Jackson Parser

时间:2015-12-08 09:59:36

标签: android json jackson

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

这是我的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的实例

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

1 个答案:

答案 0 :(得分:1)

ObjectMapper课程没有readValues()方法使用readValue()代替。

使用此:

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