简体   繁体   English

如何使用GSON解析JSON,以获取未针对不良JSON进行解析的JSONObject?

[英]How to parse a JSON using GSON, to get JSONObject where it is not parsed as desired for a bad JSON?

The question is that when I am parsing the following JSON using GSON library, I am getting GSON class in the form of POJO , where every key and value has a getter and setter. 问题是,当我使用GSON库解析以下JSON时, 将以 POJO的形式获取GSON类,其中每个键和值都有一个getter和setter。

See the example of such bad JSON below, as not parsed correctly by GSON library 请参阅下面的这种不良JSON的示例,因为GSON库未正确解析该示例

Now that is not the format I require. 现在那不是我需要的格式。 Instead, I want a hashmap object in return (Library can return ArrayList then why not TreeHashMap ?) . 相反,我希望返回一个hashmap对象(库可以返回ArrayList,然后为什么不返回TreeHashMap?) For me, this part of JSON is not parsed by GSON as I desire. 对我来说,JSON的这一部分没有按我的意愿被GSON解析。

My question is whether I should ask my back-end team to reformat the JSON as in second JSON below, or to try to use GSON library as is but extract that JSONObject programmatically from JSON where the keys are dynamic in nature, and it is uncertain whether there are a couple of such keys there or thousands of them in the JSON that is returned. 我的问题是我是否应该要求后端团队像下面的第二个JSON中一样重新格式化JSON ,还是尝试按原样使用GSON库,但是从编程中从JSON中提取该JSONObject ,而JSON本质上是动态的,因此不确定是否有几个这样的键,或者在返回的JSON中有成千上万个这样的键。

See the example of the good JSON format below, converted from the bad JSON I have provided as it can be easily parsed by GSON to the format I desire 请参阅下面的良好 JSON格式示例,该示例是从我提供的不良 JSON转换而来的,因为GSON可以轻松地将其转换为所需的格式

For this, either I have to explain a rule to my back-end team: 为此,我要么必须向后端团队解释一条规则:

RULE: 规则:

The keys should not be dynamic in nature. 这些键本质上不应是动态的。 If in case the keys are dynamic, a list of searchable keys should also be defined in the same JSON or separate MODEL JSON that should be requested in a call before this JSON is parsed (in a separate Web-Service API response). 如果密钥是动态的,则还应在解析此JSON之前在调用中请求的同一JSON或单独的MODEL JSON中定义可搜索的密钥列表(在单独的Web-Service API响应中)。

Example of bad JSON: 错误JSON的示例:

{"Grocery": [
      {
        "id": 2,
        "name": "Kirana Store",
        "parent_id": 1,
        "category_image": "",
        "category_type": "product",
        "position": 0,
        "front_status": 0,
        "slug_url": "daal-pulses",
        "created_at": "2016-06-27 06:44:59",
        "updated_at": "2018-02-13 04:55:33"
      },
      {
        "id": 3,
        "name": "Departmental Store",
        "parent_id": 1,
        "category_image": "",
        "category_type": "product",
        "position": 0,
        "front_status": 0,
        "slug_url": "dry-fruits",
        "created_at": "2016-06-27 06:45:12",
        "updated_at": "2018-02-13 04:56:01"
      }
    ]}

and there may be thousands of dynamic keys like " Grocery " say like " Stationary ", " Hosiery ", " Medicines ", or so... as categories of products. 产品类别可能有成千上万个动态键,例如“ Grocery ”,“ Stationary ”,“ Hosiery ”,“ Medicines ”等等。 GSON will make getters for each such dynamic key. GSON会为每个这样的动态密钥获取吸气剂。

Should be corrected and returned by web-service API as: 应该由网络服务API更正并返回为:

{

  "Products": [

    {

      "parent_category": "Grocery",

      "items": [

        {

          "id": 2,

          "name": "Kirana Store",

          "parent_id": 1,

          "category_image": "",

          "category_type": "product",

          "position": 0,

          "front_status": 0,

          "slug_url": "daal-pulses",

          "created_at": "2016-06-27 06:44:59",

          "updated_at": "2018-02-13 04:55:33"

        },

        {

          "id": 3,

          "name": "Departmental Store",

          "parent_id": 1,

          "category_image": "",

          "category_type": "product",

          "position": 0,

          "front_status": 0,

          "slug_url": "dry-fruits",

          "created_at": "2016-06-27 06:45:12",

          "updated_at": "2018-02-13 04:56:01"

        }

      ]

    }

  ]

}

Where there can be thousands of categories as Grocery as described above, and getters for such keys can not be computed at run time. 如上所述,这里可能有成千上万个类别的Grocery ,并且此类键的吸气剂无法在运行时计算。 But if these keys are converted to values, represented by a common key each, as in the correction above, then having a common getter for that key " parent_category " makes sense. 但是,如果将这些键转换为分别由一个公共键表示的值(如上述更正),则对该键使用一个公共的吸气剂“ parent_category ”是有意义的。 Now, this is the JSON that GSON can parse to my desired output. 现在,这是GSON可以解析为我想要的输出的JSON But the thing is that our back end team do not co-operate to change anything, because there is a re-work here. 但事实是,我们的后端团队不合作更改任何东西,因为这里有返工。 If I switch to traditional parsing, there will be a huge re-work at my end as well. 如果我改用传统的解析,那么最终的工作量也会很大。

OR I have to find out a way where I can extract the JSONObject from the JSON using GSON so that I can parse it manually. 或者,我必须找出一种方法,可以使用GSONJSON中提取JSONObject ,以便可以手动解析它。

Hence I need something to save time that will go in this re-work. 因此,我需要一些可以节省时间的东西,以便进行这项返工。

After getting JSONObject that is not parsed by GSON as desired: 根据需要获取未由GSON解析的JSONObject之后:

Getting JSONObject from JSON where it is not parsable by GSON : 从GSON无法解析的JSON中获取JSONObject

Use keySet() of JSONObject to get the keys from JSON , as Set collection. 使用JSONObject keySet()JSON获取密钥,作为Set集合。 Iterating through the keys in the Set, I am obtaining corresponding values, in the current JSON section. 遍历Set中的键,我正在当前JSON部分中获取相应的值。

Please see the example: 请参见示例:

Java Code Examples for org.json.JSONObject.keySet() org.json.JSONObject.keySet()的Java代码示例

Now if GSON can not parse such kind of JSON formats, where it gives getters and setters for each key and if the developer has to parse JSON manually, then still there is a chance of saving time, using this solution. 现在,如果GSON无法解析这种JSON格式,它为每个键提供getter和setter,并且如果开发人员必须手动解析JSON ,那么仍然可以使用此解决方案节省时间。 But till GSON make amendments as you suggest it should, you have to live with this solution. 但是,直到GSON按照您的建议进行修改之前,您必须接受此解决方案。

What should I have done if I were in GSON development team?: 如果我在GSON开发团队中应该怎么做?:

There should have been another kind of annotation where instead of getting getters and setters, one should be able to demand from the GSON library to return key-value pairs in the form of hash-map or array-list of POJO in the output. 应该有另一种注释,而不是获取getter和setter的注释,应该能够从GSON库中要求以hash-map或POJO的array-list形式在输出中返回key-value对。 This at present is not there. 目前尚不存在。

Happy Coding ;-) 快乐编码;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM