如何在Android中为以下JSON字符串创建GSON解析的对象类?

时间:2013-12-24 13:06:48

标签: android json gson

{
"errorDesc": "",
"ownerId": 2096,

"departments": "Asset Protection,Backroom Flow,Bakery,Clinic,Dairy,Deli,Distribution (Communication),Facilities Management,Frozen,Guest Experience,Human Resources,Instocks,Meat,Optical,POG and Adjacencies,Pharmacy,Photo Lab,Portrait Studio,Presentation,Price Accuracy,Produce/Floral,Remodel,Reverse Logistics,Sales Floor: A&A,Sales Floor: Grocery,Sales Floor: Hardlines,Sales Floor: Health & Beauty,Sales Floor: Home,Signing and Fixtures,Starbucks,Store,Target Cafe",

"validUser": "Y",
"clientId": "TGT",
"displayPrio": "Y",
"allowSimpleProject": "Y",
"simpleProjectType": "SIM",
"reqDept": {
    "STORE_AP": "Asset Protection",
    "STORE_BF": "Backroom Flow",
    "STORE_BAKERY": "Bakery",
    "STORE_CLINIC": "Clinic",
    "STORE_DAIRY": "Dairy",
    "STORE_DELI": "Deli",
    "STORE_DISTRIBUTI": "Distribution (Communication)",
    "STORE_FM": "Facilities Management",
    "STORE_FROZEN": "Frozen",
    "STORE_GE": "Guest Experience",
    "STORE_HR": "Human Resources",
    "STORE_INSTOCKS": "Instocks",
    "STORE_MEAT": "Meat",
    "STORE_OPTICAL": "Optical",
    "STORE_POG": "POG and Adjacencies",
    "STORE_PHARM": "Pharmacy",
    "STORE_PL": "Photo Lab",
    "STORE_PS": "Portrait Studio",
    "STORE_PRESENTATI": "Presentation",
    "STORE_PA": "Price Accuracy",
    "STORE_PROD": "Produce/Floral",
    "STORE_REMODEL": "Remodel",
    "STORE_RL": "Reverse Logistics",
    "STORE_AA": "Sales Floor: A&A",
    "STORE_GROC": "Sales Floor: Grocery",
    "STORE_SFH": "Sales Floor: Hardlines",
    "STORE_HB": "Sales Floor: Health & Beauty",
    "STORE_HOME": "Sales Floor: Home",
    "STORE_SIGNFIX": "Signing and Fixtures",
    "STORE_STARBUCKS": "Starbucks",
    "STORE": "Store",
    "STORE_TC": "Target Cafe"
},

"viewOptions": null,
"sessionId": "c170be7b-81a3-4d95-a204-af72d166d28e",
"unitName": "MPLS-NICOLLET MALL",
"userId": "dderr-st",
"userName": "Dan Derr - Store",
"errorCd": "0",
"profiles": "STL/Store Team Lead,Specialist",
"view": "",
"unitId": "1375"
}

有人可以帮助..how为上面的JSON字符串创建Object类

这样我才能做到这一点 -  Gson gson = new Gson();

        Register register_class = gson.fromJson(registration_response,Register.class);           
         register_list.add(register_class); 

4 个答案:

答案 0 :(得分:1)

虽然我强烈建议您自己了解JSON的结构以及它如何转换为标准Java对象。有些工具可以自动从JSON生成类。但是,这些工具总是可靠的!其中之一是:

http://www.jsonschema2pojo.org/

如果您要学习JSON结构,请记住以下格式:

{ “fieldName的”: “值”}

这转换为:

class RootClass
{
    private String fieldName;
    public String getFieldName() {
       return fieldName;
    }

    public void setFieldName(String fieldName) {
       this.fieldName = fieldName;
    }
}

其中fieldName将获取值:'value'。

答案 1 :(得分:1)

你的课应该看起来像:(我说的是因为我标记为字符串字段,我不确定你想要它们是什么)

public class MyClass {
    private String errorDesc;
    private Integer ownerId;
    private String departments;
    private String validUser;
    private String clientId;
    private String displayPrio;
    private String allowSimpleProject;
    private String simpleProjectType;
    private ReqDept reqDept;
    private String viewOptions;
    private String sessionId;
    private String unitName;
    private String userid;
    private String userName;
    private String errorCd;
    private String profiles;
    private String view;
    private Integer unitId;

    class ReqDept {
        private String STORE_API;
        private String STORE_BF;
        private String STORE_BAKERY;
        private String STORE_CLINIC;
        private String STORE_DAIRY;
        private String STORE_DELI;
        private String STORE_DISTRIBUTI;
        private String STORE_FM;
        private String STORE_FROZEN;
        private String STORE_GE;
        private String STORE_HR;
        private String STORE_INSTOCKS;
        private String STORE_MEAT;
        private String STORE_OPTICAL;
        private String STORE_POG;
        private String STORE_PHARM;
        private String STORE_PL;
        private String STORE_PS;
        private String STORE_PRESENTATI;
        private String STORE_PA;
        private String STORE_PROD;
        private String STORE_REMODEL;
        private String STORE_RL;
        private String STORE_AA;
        private String STORE_GROC;
        private String STORE_SFH;
        private String STORE_HB;
        private String STORE_HOME;
        private String STORE_SIGNFIX;
        private String STORE_STARBUCKS;
        private String STORE;
        private String STORE_TC;
}

您还需要为MyClass中的字段和内部ReqDept类中的字段生成getter和setter。 ReqDept类是必需的,因为您在JSON字段内获取的信息是另一个JSON对象。

另外,请注意“Y”值,因为它们不会自动转换为boolean(这就是我将它们标记为字符串的原因)。

希望这有帮助

答案 2 :(得分:1)

您应该在Android Studio中使用GsonFormat插件,它可以将json字符串转换为Java对象为Gson

转到文件 - >设置 - >插件 - >浏览存储库 - >搜索GsonFormat

并下载并重新启动android studio

现在,转到getter setter类并按alt + s并将json字符串放入打开的窗口,然后单击ok或转到设置以获取更多选项

答案 3 :(得分:0)

看一下JSON使用GSON解析的博客文章和我写的本地技术:

JSON Parsing using GSON guide

指南末尾有一个示例项目,您可以下载并学习。