当前位置: 首页 > 知识库问答 >
问题:

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

羊舌高峰
2023-03-14

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

{
  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。每个索引包含一个对象,每个对象可以包含一个自定义字段键,该键也是一个json数组,在某些情况下可以包含optionsjson数组。

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

共有1个答案

吕学
2023-03-14

您将需要使用A类,其中包含B类的一个实例。

public class A {
   private B response;
}

然后需要类B来拥有类C的一个实例

public class B {
   private C request_types;
}

然后c将包含int c_id和类的数组d以及每个其他数组的类。然后,D类将包含E类的单个对象,称为ObjectKey。类E将包含ObjectKey下的所有字段...

诸如此类...您说得对,JSON非常复杂。

 类似资料:
  • 问题内容: 我有一个很长的JSON与Gson一起解析,但是为了简洁起见,我将其修剪为以下示例: 从SO和其他几个地方,我发现我需要定义一个顶级容器,例如下面的容器,但我不知道如何完成其​​定义 然后每堂课 我正在尝试解析它,这是到目前为止我编写的代码: JSON字符串存储在名为response的变量中 我的最终要求是of,并且关联。 问题答案: 第一个问题 :您的需求是: 它不必是静态的。 第二个

  • 问题内容: 我是Java编程的新手,需要通过网络解析一个复杂的JSON对象。过去一天,我一直在阅读有关GSON的文档,但能够完全解析这种类型的结构并没有太大的运气: 我已经能够使它与该问题类似地工作,但是无法弄清楚如何使该附加数组级别起作用。 问题答案: 使用GSON的正确格式是我正在寻找的格式:

  • 问题内容: 我有一个像这样的JSON对象: 并尝试与Gson解析: 但是“ responceBean”始终为“ null” 这是所有其他类: 这是我的最新尝试。我不知何故找不到正确的方法。任何帮助将不胜感激。 问题答案: 您的 JSON模型与您的对象模型不匹配 。 您需要一个中间层来填补空白: TypeAdapter 。 而且,没有用户的命名信息。 最后是名称不匹配:JSON中的“ worklog

  • 问题内容: 我一直在寻找这个小时,却没有找到答案。在燃烧之前,请仔细阅读整个问题!:) 我有类似这样的表格: 并且需要能够将其序列化为: 我在SO上尝试过大多数答案,包括jquery-json库,并且大多数返回如下所示: 这是 我不能使用的 东西!:P 提前谢谢大家。 问题答案: 试试我为您编写的这段代码…仅使用您的数据结果,对我来说效果很好。您可以对其进行处理,并制作一个简单的jQuery插件…

  • 主要内容:示例我们将一个Java对象序列化为一个Json文件,然后读取该Json文件以获取对象。 在这个例子中,创建一个类。 然后将对象列化后存储在文件中,该文件将具有对象的json表示形式。 示例 在中创建一个名为的Java类文件,参考以下代码 - 执行上面示例代码,得到以下结果 -