开发者

JSON deserialization using GSON

开发者 https://www.devze.com 2023-01-27 06:02 出处:网络
I have the following json response from a url: stdClass Object ( [uname] => Eamorr [phoneNumber] => 082732938293

I have the following json response from a url:

stdClass Object
(
    [uname] => Eamorr
    [phoneNumber] => 082732938293
    [fname] => Steve
    [lname] => Hearst
    [sex] => M
    [roofSignNumber] => 230948
    [vehicleReg] => 07D2892
    [vehicleMake] => Toyota
    [vehicleModel] => Avensis
    [vehicleNumPassengers] => 4
    [profilePic] => -1
    [online] => 1
    [status] => 
    [picList] => Array
        (
        )

    [lat] => 
    [lng] => 
    [reputation] => 0.63
    [numPagesComments] => 1
    [last5comments] => Array
        (
            [0] => stdClass Object
                (
                    [comment] => hello5
                    [fromUname] => Eamorr
                    [profilePic] => -1
                    [time] => 1290254763
                    [id] => 23628mr28018onm647z2
                )

        )

)

And here's how I'm doing the parse:

                      Gson json=new Gson();
                      try{
                          Driver driver=json.fromJson(response,Driver.class);
                          Log.i("json",driver.profilePic);
                      }catch(JsonParseException e){
                          Log.i("error","JsonParseException");
                      }

When I run the code, I get this error: ERROR/AndroidRuntime(823): java.lang.RuntimeException: No-args constructor for class com.project.driver.Driver$Post does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

I'm having trouble with defining a suitable Driver.java for the parse. When I omit the 'last5comments' field, everything works fine, it's just the 'last5comments' bit I'm having trouble with...

Here is Driver.java:

public class Driver {
    public String uname="";
    public String phoneNumber="";
    publ开发者_开发问答ic String fname="";
    public String lname="";
    public String sex="";
    public String roofSignNumber="";
    public String vehicleReg="";
    public String vehicleMake="";
    public String vehicleModel="";
    public String vehicleNumPassengers="";
    public String profilePic="";
    public String online="";
    public String status="";
    public String[] picList;
    public String lat="";
    public String lng="";
    public String reputation="";
    public String numPagesComments="";

    //having problems below this line 
    public Post[] last5comments;

    public class Post{
        public String comment="";
        public String fromUname="";
        public String profilePic="";
        public String time="";
        public String id="";
    }
}

Any help with defining a suitable Driver.java is much appreciated.

Thanks in advance,


I've managed to resolve this problem by looking at:

http://sites.google.com/site/gson/gson-user-guide#TOC-Nested-Classes-including-Inner-Clas

(Section on "Nested Classes (including Inner Classes)")

All I had to do was make the Post object static.

public static class Post{
        public String comment="";
        public String fromUname="";
        public String profilePic="";
        public String time="";
        public String id="";
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号