4

虽然这个主题有几个线程..请不要将其标记为重复。

我的 pojo 看起来像这样:

public class sample {


    public sample() {
        // TODO Auto-generated constructor stub
    }

    private String instructions;
    private String resource;
    private List<Map<String,String>> fields;
    private String taskid;

    private List<Map<String,String>> answer;

    public String getTaskid() {
        return taskid;
    }
    public void setTaskid(String taskid) {
        this.taskid = taskid;
    }
    public String getInstructions() {
        return instructions;
    }
    public void setInstructions(String instructions) {
        this.instructions = instructions;
    }
    public String getResource() {
        return resource;
    }
    public void setResource(String resource) {
        this.resource = resource;
    }
    public List<Map<String,String>> getFields() {
        return fields;
    }
    public void setFields(List<Map<String,String>> fields) {
        this.fields = fields;
    }
    public List<Map<String,String>> getAnswer() {
        return answer;
    }
    public void setAnswer(List<Map<String,String>> answer) {
        this.answer = answer;
    }



}

我正在做一个 httpget,结果是一个 Json 对象数组,我尝试对其进行类型转换以进行采样,但它给出了一个异常。

反序列化片段如下

sample[] temp = gsonObj.fromJson(response, sample[].class);

我得到的例外是

java.lang.RuntimeException: Unable to invoke no-args constructor for class [sample;. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.MappedObjectConstructor.constructWithAllocators(MappedObjectConstructor.java:68)
    at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:52)
    at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:42)
    at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:60)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:104)
    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:76)
    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)
    at com.google.gson.Gson.fromJson(Gson.java:551)
    at com.google.gson.Gson.fromJson(Gson.java:498)
    at com.google.gson.Gson.fromJson(Gson.java:467)
    at com.google.gson.Gson.fromJson(Gson.java:417)
    at com.google.gson.Gson.fromJson(Gson.java:389)
    at HTTPClientUtils.getResultsFromMobileWorks(HTTPClientUtils.java:327)

你能告诉我我在哪里犯错吗??

4

2 回答 2

2

无法重现。但这里有什么作用:

public class Sample {

    public Sample(){}
    public int kk;
    public List<Map<String,String>> fields;


    public static void main(String[] args) {
        String s = "[{\"kk\":1, \"fields\":[{\"a\":\"a1\"}]}, {\"kk\":5}, {\"kk\":2}, {\"kk\":8}, {\"kk\":6, \"fields\":[{\"b\":\"b1\"}]}]";
        Sample[] r = new Gson().fromJson(s, Sample[].class);
        for(Sample t: r)
            System.out.println(">> " + t.kk + " " + t.fields);
    }

}

结果:

>> 1 [{a=a1}]
>> 5 null
>> 2 null
>> 8 null
>> 6 [{b=b1}]

边注:

  1. 始终将您的课程大写。
  2. 发布 sscce
于 2012-10-17T04:48:06.510 回答
0

您必须使用TypeTokenGoogle 的课程。
您当然需要一个通用类T来使其工作,如下所示:

Type fooType = new TypeToken<Foo<Bar>>() {}.getType();

gson.fromJson(json, fooType);
于 2013-02-22T09:21:48.470 回答