使用GSON解析/序列化复杂的json对象

时间:2013-08-30 02:31:40

标签: android json parsing mapping gson

这个json对象有时会非常大,我想用GSON解析它,但我不太清楚我的java对象的格式应该如何解析它们。

对我来说真正有用的是一些非常上下文的例子,给定这个对象,我将如何形成我的java模型对象来保存gson.fromJSON方法中的数据?我当前的对象被填充为“null”

我在底部解释了对象的简洁性

{
  response: {
    user_is_following: 0,
    name: "Tennesee",
    submitted_requests: 429,
    completed_requests: 34,
    request_types: {
        c_id: 1064,
        request_types: [
            {
                objectKey: {
                    id: 15699,
                    name: "Complaint",
                    has_custom_fields: 0,
                    disable_title: 0,
                    disable_description: 0,
                    force_private: 0,
                    image: null
                }
            },
            {
                objectKey: {
                    id: 15700,
                    name: "Compliment",
                    has_custom_fields: 0,
                    category_id: 605,
                    disable_title: 0,
                    disable_description: 0,
                    force_private: 0,
                    image: null
                }
            },
            {
                objectKey: {
                    id: 17574,
                    name: "Custom Fields, all of them",
                    has_custom_fields: 1,
                    disable_title: 0,
                    disable_description: 0,
                    force_private: 0,
                    image: null,
                    custom_fields: [
                        {
                            custom_field: {
                                id: "1663",
                                name: "I'm a text input",
                                description: "I'm a text input description",
                                type: "text",
                                required: 1,
                                is_public: 1,
                                options: [

                                ]
                            }
                        },
                        {
                            custom_field: {
                                id: "1664",
                                name: "I'm a text input display only",
                                description: "I'm a text input display only description",
                                type: "display",
                                required: 0,
                                is_public: 0,
                                options: [

                                ]
                            }
                        },
                        {
                            custom_field: {
                                id: "1665",
                                name: "I'm a checkbox",
                                description: "I'm a checkbox description",
                                type: "checkbox",
                                required: 0,
                                is_public: 1,
                                options: [

                                ]
                            }
                        },
                        {
                            custom_field: {
                                id: "1666",
                                name: "I'm a single select",
                                description: "I'm a single select description",
                                type: "singleselect",
                                required: 1,
                                is_public: 0,
                                options: [
                                    {
                                        option: {
                                            id: "3751",
                                            name: "A 123 !@@#",
                                            description: "A 123 !@@# description"
                                        }
                                    },
                                    {
                                        option: {
                                            id: "3752",
                                            name: "B ",
                                            description: "B description"
                                        }
                                    },
                                    {
                                        option: {
                                            id: "3753",
                                            name: "C",
                                            description: "C description"
                                        }
                                    },
                                    {
                                        option: {
                                            id: "3754",
                                            name: " D",
                                            description: "D description"
                                        }
                                    }
                                ]
                            }
                        },

                    }
                ],
                s_types: [

                ],
                categories: [
                    {
                        category: {
                            id: 618,
                            client: 1064,
                            name: "Abc",
                            gov_creator: 1841,
                            description: "",
                            parent: 607,
                            date_created: 1368137256,
                            image: null
                        }
                    },
                    {
                        category: {
                            id: 602,
                            client: 1064,
                            name: "Animal Control",
                            gov_creator: 2275,
                            description: "",
                            parent: null,
                            date_created: 1367878768,
                            image: null
                        }
                    },

                }
            ],
            assets: [

            ],
            benchmark: 0.36078095436096
        },
        status: {
            type: "success",
            message: "Success",
            code: 200,
            code_message: "Ok"
        }
    }
}

真正的肉在request_types键,第二个,它是JSONArray。每个索引都包含一个对象,每个对象都可以包含一个Custom Fields键,它也是一个json数组,在某些情况下可以包含一个options json数组。

我有所有这些的模型,用于不同的解析范例,但不适用于GSON。由于内存限制,我现在需要使用GSON

1 个答案:

答案 0 :(得分:1)

您需要让我们说类A,其中包含一个类B的实例。

public class A {
   private B response;
}

然后您需要班级B才能拥有1个班级C

的实例
public class B {
   private C request_types;
}

然后C将包含int c_id和类D的数组以及每个其他数组的类。然后,类D将包含名为E的类objectKey的单个对象。类E将包含objectKey ...

下的所有字段

所以等等......你是对的,JSON是疯狂的错综复杂。