如何编写Java Bean以在Android中使用Gson解析此JSON字符串?

时间:2013-09-22 12:10:57

标签: java android json gson

我从Web服务获得了以下JSON结构:

{
  "owner": {
    "username": "bobby",
    "password": "$2a$10$KTouvpAZwyUiCOsvMej.i.zNcGVt94d.n64MHWyxX35ZR03tWCIui",
    "email": "bobby@email.com",
    "name": "Bobby P",
    "_id": "51a59d917997582c3a000003",
    "__v": 0,

    "phones": [
      "11118012888",
      "12341234",
      "987655775"

    ],
    "altemails": [
      "a@a.com",
      "b@b.com",
      "c@c.com"
    ]

  },

  "facilities": [
    {
      "url": "/v1/facilities/51a5a6237997582c3a000006"
    },
    {
      "url": "/v1/facilities/51a5a6237997582c3a000007"
    },
    {
      "url": "/v1/facilities/51a5b533ebcefa893d000003"
    },
    {
      "url": "/v1/facilities/51b0238edadae8024f000003"
    }
  ]
}

这个JSON响应。我只需要设施阵列。

有谁能告诉我如何编写Bean类,以便我可以在Android中使用Gson阅读它?

1 个答案:

答案 0 :(得分:2)

你需要这个:

package stackoverflow.questions.q18943680;

import java.util.ArrayList;

public class Owner {

    String username;
    String password;
    String email;
    String name;
    String _id;
    int  __v;

    ArrayList<String> phones;

    @Override
    public String toString() {
        return "Owner [username=" + username + ", password=" + password
                + ", email=" + email + ", name=" + name + ", _id=" + _id
                + ", __v=" + __v + ", phones=" + phones + "]";
    }

}

这样:

package stackoverflow.questions.q18943680;

public class Facility {
    String url;

    @Override
    public String toString() {
        return "Facility [url=" + url + "]";
    }
}

和此:

package stackoverflow.questions.q18943680;

import java.util.ArrayList;

public class TestBean {

    public Owner owner;
    public ArrayList<Facility> facilities;

    @Override
    public String toString() {
        return "TestBean [owner=" + owner + ", facilities=" + facilities + "]";
    }


}

您将获得结果:

package stackoverflow.questions.q18943680;

import com.google.gson.Gson;

public class Q18943680 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String json = "{"+
          "  \"owner\": {                                                                     "+
          "  \"username\": \"bobby\",                                                         "+
          "  \"password\": \"$2a$10$KTouvpAZwyUiCOsvMej.i.zNcGVt94d.n64MHWyxX35ZR03tWCIui\",  "+
          "  \"email\": \"bobby@email.com\",                                                  "+
          "  \"name\": \"Bobby P\",                                                           "+
          "  \"_id\": \"51a59d917997582c3a000003\",                                           "+
          "  \"__v\": 0,                                                                      "+
          "                                                                                   "+
          "  \"phones\": [                                                                    "+
          "    \"11118012888\",                                                               "+
          "    \"12341234\",                                                                  "+
          "    \"987655775\"                                                                  "+
          "                                                                                   "+
          "  ],                                                                               "+
          "  \"altemails\": [                                                                 "+
          "    \"a@a.com\",                                                                   "+
          "    \"b@b.com\",                                                                   "+
          "    \"c@c.com\"                                                                    "+
          "  ]                                                                                "+
          "                                                                                   "+
          "},                                                                                 "+
          "                                                                                   "+
          "\"facilities\": [                                                                  "+
          "  {                                                                                "+
          "    \"url\": \"/v1/facilities/51a5a6237997582c3a000006\"                           "+
          "  },                                                                               "+
          "  {                                                                                "+
          "    \"url\": \"/v1/facilities/51a5a6237997582c3a000007\"                           "+
          "  },                                                                               "+
          "  {                                                                                "+
          "    \"url\": \"/v1/facilities/51a5b533ebcefa893d000003\"                           "+
          "  },                                                                               "+
          "  {                                                                                "+
          "    \"url\": \"/v1/facilities/51b0238edadae8024f000003\"                           "+
          "  }                                                                                "+
          "]}                                                                                  ";


        Gson g = new Gson();
        TestBean tb = g.fromJson(json, TestBean.class);

        System.out.println("This is the result: "+tb.facilities);

    }

}

这会给你:

This is the result: [Facility [url=/v1/facilities/51a5a6237997582c3a000006], Facility [url=/v1/facilities/51a5a6237997582c3a000007], Facility [url=/v1/facilities/51a5b533ebcefa893d000003], Facility [url=/v1/facilities/51b0238edadae8024f000003]]

我建议您使用http://json.parser.online.fr/快速解析您的JSON,以获得有关如何将内容安排到JSON中以构建其他bean的直观描述。