Gson : Unable to invoke no-args constructor for class

Although there are several threads on this topic.. please do not mark this as duplicate.

My pojo looks like this :

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;
    }



}

I am doing a httpget and the result is an array of Json objects I try to typecast it to sample but it gives an exception.

the deserialization snippet is as follows

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

the exception i get is

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)

Can you please let me know where i am making the mistake ??

share improve this question
 
 
There is nothing wrong with the code you have posted, and your syntax is correct; it should work just fine. I highly suspect the code you are executing is not the code you have listed. Rebuild your project. –  Brian Roach  Oct 17 '12 at 4:47
 
you should edit and remove your company-name, and project name. Just for anonymity. –   Nishant  Oct 17 '12 at 4:51
 
Isn't Sample a nested class? Didn't the constructor throw an exception? –   maaartinus  Oct 17 '12 at 5:41

2 Answers

up vote 2 down vote accepted

Can't reproduce. But here what works:

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);
    }

}

results:

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

Sidenote:

  1. Capitalize your classes, always.
  2. post a sscce
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值