首页 > 解决方案 > 我有一个 json 但不知道如何解析它

问题描述

该数组有一个名为children的字符串,在那个children里面可以有另一个children,在那个children里面还有另一个children。请帮助我如何解析这个json的格式是这样的,我有一个json响应,其中array's inside array's。我想获取给定标签的所有值。

{
    "id": "fe13e84e-fa26-46fb-bd39-6b581dad9eb7",
    "type": "data",
    "title": "root",
    "children": [
{
    "id": "d6426ce9-e243-40b1-93f5-a1aaa3193a4c",
    "type": "group",
    "title": "first",
    "children": [
     {
    "id": "b3d888b1-c4f0-4337-87a3-d51961d81c0b",
    "type": "class",
    "title": "A",
    "children": [
      {
        "id": "df0a218d-7a08-4295-abb7-e0bdfb835414",
        "type": "color",
        "title": "red",
        "children": [
          {
            "id": "7c451766-5f91-48f5-8db5-868e7cc95905",
            "type": "taste",
            "title": "sour",
            "children": [
              {
                "id": "2567b1f1-2662-48fd-a487-167e514ce5d8",
                "type": "size",
                "title": "tiny"
              },
              {
                "id": "29a73327-d5be-44cc-8c1d-e45ddb8be2b7",
                "type": "size",
                "title": "small"
              },
              {
                "id": "718d54cf-ce19-44e9-9a94-3214ef482dc2",
                "type": "size",
                "title": "medium"
              },
              {
                "id": "f1f81f3a-be49-411d-b176-0d1e67f18864",
                "type": "size",
                "title": "large"
              },
              {
                "id": "ccbe91ef-7dec-4dc7-bbf5-ef79161670df",
                "type": "size",
                "title": "huge"
              }
            ]
          },
          {
            "id": "7b9ae76c-d189-4b44-9ea1-ea38b05d35ae",
            "type": "taste",
            "title": "bitter",
            "children": [
              {
                "id": "42b7f578-e907-475d-beb3-d1d53af1bec9",
                "type": "size",
                "title": "tiny"
              },
              {
                "id": "acaed672-d5df-43a2-9e30-5cf55f74b1ce",
                "type": "size",
                "title": "small"
              },
              {
                "id": "d4785d0b-9116-4361-8349-505934ceb9c9",
                "type": "size",
                "title": "medium"
              },
              {
                "id": "7ee5494f-dc36-45e2-bc09-2ac948133523",
                "type": "size",
                "title": "large"
              },
              {
                "id": "0ded484b-78f2-497e-bdfe-c9bd2ba78368",
                "type": "size",
                "title": "huge"
              }
            ]
          },
}

标签: androidparsing

解决方案


如果 JSON 有效,您可以通过创建 POJO 轻松获取所需的所有值,您可以通过 POJO 生成器工具生成。你甚至不需要解析 JSON。只需通过 GSON 库将 JSON 映射到 Java POJO 类并使用它。

POJO 生成器 -> http://www.jsonschema2pojo.org/


推荐阅读