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

普拉文·摩尔(Pravin Mohol)

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

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

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

    ],
    "altemails": [
      "[email protected]",
      "[email protected]",
      "[email protected]"
    ]

  },

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

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

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

Giampaolo

你需要这个:

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\": \"[email protected]\",                                                  "+
          "  \"name\": \"Bobby P\",                                                           "+
          "  \"_id\": \"51a59d917997582c3a000003\",                                           "+
          "  \"__v\": 0,                                                                      "+
          "                                                                                   "+
          "  \"phones\": [                                                                    "+
          "    \"11118012888\",                                                               "+
          "    \"12341234\",                                                                  "+
          "    \"987655775\"                                                                  "+
          "                                                                                   "+
          "  ],                                                                               "+
          "  \"altemails\": [                                                                 "+
          "    \"[email protected]\",                                                                   "+
          "    \"[email protected]\",                                                                   "+
          "    \"[email protected]\"                                                                    "+
          "  ]                                                                                "+
          "                                                                                   "+
          "},                                                                                 "+
          "                                                                                   "+
          "\"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的直观描述。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章