当前位置: 首页 > 面试题库 >

将JSON转换为类似于Flutter中的android的POJO(对象)

蒋茂材
2023-03-14
问题内容

我只是想找到一种方法来将json响应(从REST
API)转换为POJO(在android中使用),这样我就可以将接收到的数据使用到我的应用程序中了,因为使用Map不会足够m接收有点复杂,包含3个级别的数组(数组内部的数组在数组内部的数组)。

我正在使用Dio库进行api调用,我可以成功进行API调用,并在控制台上毫无问题地打印数据。谁能协助我达成目标?

以下是我得到的答复。

{
"status": 200,
"message": "success",
"data": [
    {
        "menu": {
            "megamenu_id": "55",
            "language_id": "1",
            "title": "Mobiles",
            "description": "",
            "submenu": [
                {
                    "0": {
                        "info": {
                            "megamenu_id": "67",
                            "language_id": "1",
                            "title": "Mobile Phones",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "68",
                                "language_id": "1",
                                "title": "Android Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "69",
                                "language_id": "1",
                                "title": "Smart Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "70",
                                "language_id": "1",
                                "title": "Windows Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "72",
                                "language_id": "1",
                                "title": "Basic Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "73",
                                "language_id": "1",
                                "title": "Latest Mobiles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "74",
                                "language_id": "1",
                                "title": "Upcoming Mobiles",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "75",
                            "language_id": "1",
                            "title": "Brands",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "76",
                                "language_id": "1",
                                "title": "Samsung",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "77",
                                "language_id": "1",
                                "title": "Apple",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "78",
                                "language_id": "1",
                                "title": "Redmi",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "79",
                                "language_id": "1",
                                "title": "Nokia",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "81",
                                "language_id": "1",
                                "title": "Sony",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "82",
                                "language_id": "1",
                                "title": "Micromax",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "83",
                                "language_id": "1",
                                "title": "HTC",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "84",
                                "language_id": "1",
                                "title": "Motorola",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "85",
                                "language_id": "1",
                                "title": "Oppo",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "86",
                                "language_id": "1",
                                "title": "Vivo",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "87",
                                "language_id": "1",
                                "title": "Honor",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "88",
                                "language_id": "1",
                                "title": "Oneplus",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "94",
                            "language_id": "1",
                            "title": "Mobile Accessories",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "95",
                                "language_id": "1",
                                "title": "Bluetooth Headsets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "96",
                                "language_id": "1",
                                "title": "Mobile Chargers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "97",
                                "language_id": "1",
                                "title": "Mobiles Cases & Covers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "98",
                                "language_id": "1",
                                "title": "Screen Protector",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "99",
                                "language_id": "1",
                                "title": "Mobile Batteries",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "100",
                                "language_id": "1",
                                "title": "Headphones & Headsets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "101",
                                "language_id": "1",
                                "title": "Memory Cards",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "102",
                                "language_id": "1",
                                "title": "Power Banks",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "103",
                                "language_id": "1",
                                "title": "Stylus",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "104",
                                "language_id": "1",
                                "title": "Mobile Docks",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "107",
                            "language_id": "1",
                            "title": "Features",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "108",
                                "language_id": "1",
                                "title": "Dual Sim Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "111",
                                "language_id": "1",
                                "title": "4GB Ram Mobiles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "112",
                                "language_id": "1",
                                "title": "Rear Camera [13 MP]",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "113",
                                "language_id": "1",
                                "title": "Screen Size [4.5 - 5.2 Inch]",
                                "description": ""
                            }
                        }
                    ]
                }
            ]
        }
    },

    {
        "menu": {
            "megamenu_id": "168",
            "language_id": "1",
            "title": "Fashion",
            "description": "",
            "submenu": [
                {
                    "0": {
                        "info": {
                            "megamenu_id": "169",
                            "language_id": "1",
                            "title": "Clothing",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "170",
                                "language_id": "1",
                                "title": "T-Shirts",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "171",
                                "language_id": "1",
                                "title": "Shirts",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "178",
                            "language_id": "1",
                            "title": "Womens Clothing",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "187",
                                "language_id": "1",
                                "title": "Night Wear",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "188",
                                "language_id": "1",
                                "title": "Tunics",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "189",
                                "language_id": "1",
                                "title": "Kurtis",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "190",
                                "language_id": "1",
                                "title": "Sarees",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "191",
                                "language_id": "1",
                                "title": "Lehengas",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "192",
                                "language_id": "1",
                                "title": "Swimming Suit",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "193",
                            "language_id": "1",
                            "title": "Men's Footwear",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "194",
                                "language_id": "1",
                                "title": "Shoes",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "195",
                                "language_id": "1",
                                "title": "Sneakers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "196",
                                "language_id": "1",
                                "title": "Sandals & Floaters",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "197",
                                "language_id": "1",
                                "title": "Slippers & Flip Flops",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "198",
                                "language_id": "1",
                                "title": "Loafers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "199",
                                "language_id": "1",
                                "title": "Boots",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "200",
                                "language_id": "1",
                                "title": "Shoe Care",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "201",
                            "language_id": "1",
                            "title": "Women's Jewellery",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "202",
                                "language_id": "1",
                                "title": "Earrings",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "203",
                                "language_id": "1",
                                "title": "Bangles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "204",
                                "language_id": "1",
                                "title": "Bracelets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "205",
                                "language_id": "1",
                                "title": "Rings",
                                "description": ""
                            }
                        }
                    ]
                }
            ]
        }
    },
    {
        "menu": {
            "megamenu_id": "220",
            "language_id": "1",
            "title": "Care",
            "description": ""
        }
    },
    {
        "menu": {
            "megamenu_id": "221",
            "language_id": "1",
            "title": "Offers",
            "description": ""
        }
    },
    {
        "menu": {
            "megamenu_id": "222",
            "language_id": "1",
            "title": "News",
            "description": ""
        }
    }
]
}

问题答案:

因此,在尝试使用json_serializable库后,我想出了自己的解决方案,它根本不需要任何外部库,并且像一个魅力一样工作。这样,我必须编写更少的样板代码,我认为这是一种干净的方法。

这是制作模型的方法

class SideMenuRes {
final int status;
final String message;
final List<SideMenuDatum> sideMenuData;

SideMenuRes({this.status, this.message, this.sideMenuData});
factory SideMenuRes.fromJson(Map json) {
  return SideMenuRes(
    status: json['status'],
    message: json['message'],
    sideMenuData: json['status'] == 200 ? (json['data'] as List).map((i) => new 
  SideMenuDatum.fromJson(i)).toList() : null,
  );
}
}

class SideMenuDatum {
final Menu menu;
SideMenuDatum({this.menu});
factory SideMenuDatum.fromJson(Map json) {
  return SideMenuDatum(
    menu: Menu.fromJson(json['menu']),
  );
}
}

class Menu {
final String megamenu_id;
final String language_id;
final String title;
final String description;
final List<SubMenu> subMenu;

Menu({this.megamenu_id, this.language_id, this.title, this.description, this.subMenu});
factory Menu.fromJson(Map json) {
  return Menu(
      megamenu_id: json['megamenu_id'],
      language_id: json['language_id'],
      title: json['title'],
      description: json['description'],
      subMenu: json['submenu'] != null ? (json['submenu'] as List).map((i) => new SubMenu.fromJson(i)).toList() : null
  );
}
}

class SubMenu {
final Zero zero;
final List<SubSubMenu> subSubMenu;

SubMenu({this.zero, this.subSubMenu});
factory SubMenu.fromJson(Map json) {
  return SubMenu(
      zero: Zero.fromJson(json['0']),
      subSubMenu: (json['subsubmenu'] as List).map((i) => new SubSubMenu.fromJson(i)).toList()
  );
}
}

class Zero {
final Info info;
Zero({this.info});
factory Zero.fromJson(Map json) {
  return Zero(
    info: Info.fromJson(json['info']),
  );
  }
 }

class SubSubMenu {
final InfoSub infoSub;
SubSubMenu({this.infoSub});

factory SubSubMenu.fromJson(Map json) {
  return SubSubMenu(
      infoSub: InfoSub.fromJson(json['infosub'])
  );
}
}

class InfoSub {
final String megamenu_id;
final String language_id;
final String title;
final String description;

InfoSub({this.megamenu_id, this.language_id, this.title, this.description});
factory InfoSub.fromJson(Map json) {
return InfoSub(
    megamenu_id: json['megamenu_id'],
    language_id: json['language_id'],
    title: json['title'],
    description: json['description']
  );
 }
}

class Info {
 final String megamenu_id;
 final String language_id;
final String title;
final String description;

Info({this.megamenu_id, this.language_id, this.title, this.description});
factory Info.fromJson(Map json) {
return Info(
    megamenu_id: json['megamenu_id'],
    language_id: json['language_id'],
    title: json['title'],
    description: json['description']
  );
 }
}

并这样称呼它

SubMenuRes subMenuRes = SubMenuRes.fromJson(response.data);

而已!



 类似资料:
  • 问题内容: 我正在将Jersey用于REST WS,并且得到的响应为JSON。 我想将此响应转换为POJO。怎么做 ? 问题答案: 要在Java和JSON之间进行转换,有很多可供选择的API 。 您可以“手动”遍历JSON组件并提取值以填充Java对象,或者可以使用JSON到Java的绑定API来解决许多低级映射问题。

  • 我想在我的JS项目中使用类似Java-POJO的类:只有私有字段,getters和setters。 我会在JSON字符串中转换我的类似Java-POJO的类,以将其发送到服务器。我不能使用JSON.stringify(myClass),因为这些字段是私有的。所以我认为使用这样的递归方法... ...但是在我花了很多时间写它之前,我想问你是否存在一种更好的方法来转换JSON中的Java-POJO类对

  • 我尝试使用DTO到JSON(在JSON文件中写入)和JSON到DTO(从JSON文件中读取)作为常用方法(不同pojo写入/读取操作使用的通用方法) 为了作为通用方法使用,我使用返回类型作为对象。 在我的代码下面 提前道谢。

  • 我有这样一个json: 所以它是一个包含名称数组的对象。对此,什么是等效的Pojo? 但是我得到嵌套异常:

  • 我正在尝试将JSON转换为POJO类。这个JSON是我从第三方REST API调用得到的,我想把它转换成POJO类。为此,我使用jackson databind jar,下面是我代码的一部分。 这里现在不是POJO类,我声明了Object类型的ModelObjcet变量,我的问题是我们是否需要在将JSON转换为POJO之前创建带有必填字段和getter setter方法的POJO类? 如果是,那么

  • 问题内容: 我正在用PHP开发Web应用程序, 我需要将许多对象作为JSON字符串从服务器传输,是否存在用于PHP的任何库,以将对象转换为JSON,并将JSON字符串转换为Objec,例如Java的Gson库。 问题答案: 这应该可以解决问题! 这是一个例子 如果您希望输出为数组而不是对象,则传递给 有关json_encode()的更多信息 另请参阅:json_decode()