简体   繁体   English

无法使用GSON库解析JSON文件

[英]Unable to parse JSON file using GSON library

I am unable to parse this JSON file. 我无法解析此JSON文件。 Any suggestions are appreciated. 任何建议表示赞赏。 I have created POJO classes from http://www.jsonschema2pojo.org/ I tried JSON deserilization solution mentioned but it did not work out for me. 我已经从http://www.jsonschema2pojo.org/创建了POJO类,我尝试了提到的JSON反序列化解决方案,但对我来说却行不通。

    {
  "error": "",
  "data": [
    {
      "view": "Viewpapger",
      "data": [
        {
          "view": "ImageView",
          "data": {
            "url": "random.jpg"
          },
          "properties": {
            "width": "fill_parent",
            "height": "500"
          }
        },
        {
          "view": "Textview",
          "data": {
            "text": "afvnjkafvf"
          },
          "properties": {
            "width": "fill_parent"
          }
        },
        {
          "view": "Textview",
          "data": {
            "text": "afvnjkafvf"
          },
          "properties": {
            "width": "fill_parent"
          }
        },
        {
          "view": "ImageView",
          "data": {
            "url": "random.jpg"
          },
          "properties": {
            "width": "fill_parent",
            "height": "500"
          }
        }
      ],
      "properties": {
        "width": "wrap_content",
        "height": "500"
      }
    },
    {
      "view": "Textview",
      "data": {
        "text": "afvnjkafvf"
      },
      "properties": {
        "width": "fill_parent"
      }
    },
    {
      "view": "ImageView",
      "data": {
        "url": "random.jpg"
      },
      "properties": {
        "width": "fill_parent",
        "height": "500"
      }
    }
  ]
}

I think the problem here is that the JSON structure does not correspond to any strictly specified schema but seems more like an ad-hoc collection of keys and values. 我认为这里的问题是JSON结构不与任何严格指定的架构相对应,而是更像是键和值的临时集合。 For example, (it seems that) depending on the value of the view field in your view description object the data field can be either 例如,(似乎)取决于视图描述对象中view字段的值, data字段可以是

  • a JSON array containing other view descriptions (for "Viewpager") 包含其他视图描述的JSON数组(用于“ Viewpager”)
  • a JSON object containing a string called "url" (for "ImageView") 包含名为“ url”(用于“ ImageView”)的字符串的JSON对象
  • a JSON object containing a string called "text" (for "TextView") 包含名为“ text”(用于“ TextView”)的字符串的JSON对象

So, basically it seems that data can be anything and moreover that you could have an arbitrarily deep tree of nodes where data could be anything at any level in the tree. 因此,基本上看来data可以是任何东西 ,而且您可以拥有任意深度的节点树,其中data可以是树中任何级别的任何东西。 (It may be different though if, for example, you can always be certain that the top level element is a "Viewpager" that contains one level of sub-views like "ImageView" or "TextView" - if you have any guarantees like that let me know). ((例如,如果您始终可以确定顶层元素是“ Viewpager”,其中包含一层子视图,例如“ ImageView”或“ TextView”,则可能会有所不同-如果您有类似的保证,让我知道)。

In that case I think you are better off parsing the JSON in some other, less strict way (maybe using org.json.JSONArray and org.json.JSONObject ) and then handling all the different cases "manually". 在那种情况下,我认为您最好以其他不太严格的方式解析JSON(也许使用org.json.JSONArrayorg.json.JSONObject ),然后“手动”处理所有不同的情况。

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

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