如何快速处理ObjectMapper中的null?

问题描述

我正在使用ObjectMapper快速解析json,但由于“ null”,它在child_menu上失败了。解析在child_menu上停止,并且child_menu下方的其余字段分配有空值或默认值。有什么办法可以解决吗?下面是模型代码。请帮忙,谢谢-

响应json-

{
               "category_creation_id":"10103","category_id":"1010300000","template_type":"category","list_type":"block-display","menu_set_menu_id":"288","menu_set_id":"4","menu_id":"1030300000","menu_creation_id":"10303","menu_name":"Results","menu_level":"2","num_of_siblings":"0","hierarchical_name":"Companies->Results","menu_parent_id":"222","is_leaf":"N","mobile_display_order":"2","menu_options":"0","is_active":"Y","menu_font_color":null,"menu_background_color":null,"hover_menu_font_color":null,"hover_menu_background_color":null,"menu_url":"/companies-results","is_special_menu":"N","new_article_count":"0","child_menu":null
            }

响应字典-

{
                    "category_creation_id" = 103;
                    "category_id" = 1030000000;
                    "child_menu" = "<null>";
                    "hover_menu_background_color" = "<null>";
                    "hover_menu_font_color" = "<null>";
                    "is_active" = Y;
                    "is_leaf" = Y;
                    "is_special_menu" = N;
                    "list_type" = "block-display";
                    "menu_background_color" = "<null>";
                    "menu_creation_id" = 10103;
                    "menu_font_color" = "<null>";
                    "menu_id" = 1010300000;
                    "menu_level" = 2;
                    "menu_name" = Test;
                    "menu_options" = 0;
                    "menu_parent_id" = 211;
                    "menu_set_id" = 4;
                    "menu_set_menu_id" = 214;
                    "mobile_display_order" = 5;
                    "new_article_count" = 0;
                    "num_of_siblings" = 0;
                }

struct MenuModel: Mappable {
    // Variable Initializer 
    var menuFontColor: String?
    var menuBackgroundColor: String?
    var hovermenufontcolor : String?
    var menuName = ""
    var hierachyName = ""
    var isActive = false
    var isLeaf = false
    var isSpecialMenu = false
    var menuCreationId = 0
    var menuId: Int64 = 0
    var categoryId: Int64 = 0
    var menuLevel = 0
    var menuOptions = 0
    var menuParentId = 0
    var menuSetId = 0
    var menuSetMenuId = 0
    var menuUrl = ""
    var mobileDisplayOrder = 1
    var numberOfSiblings = 0
    var isExpanded = false
    var subMenus = [MenuModel]()
    var templateType: MenuTemplateType?
    var newArticleCount: Int?
    var listType: String?
    
    init?(map: Map) {
     
    }
  
    
    init() {
    }
    
    init(id: Int64,title: String) {
        self.menuName = title
        self.menuId = id
    }
    /// For mapping of variables with their respective API keys
    /// - Parameter map: Map type object
    mutating func mapping(map: Map) {
        menuName <- map[APIKey.menuName]
        hierachyName <- map[APIKey.hierarchicalName]
        isActive = (map[APIKey.isActive].currentValue as? String ?? "").uppercased() == "Y"
        isLeaf = (map[APIKey.isLeaf].currentValue as? String ?? "").uppercased() == "Y"
        isSpecialMenu = (map[APIKey.isSpecialMenu].currentValue as? String ?? "").uppercased() == "Y"
        menuCreationId <- map[APIKey.menuCreationId]
        menuId <- map[APIKey.menuId]
        categoryId <- map[APIKey.categoryId]
        menuFontColor <- map[APIKey.menuFontColor]
        menuBackgroundColor <- map[APIKey.menuBackgroundColor]
        hovermenufontcolor <- map[APIKey.hovermenufontcolor]
        menuLevel <- map[APIKey.menuLevel]
        menuOptions <- map[APIKey.menuOptions]
        menuParentId <- map[APIKey.menuParentId]
        menuSetId <- map[APIKey.menuSetId]
        menuSetMenuId <- map[APIKey.menuSetMenuId]
        menuUrl <- map[APIKey.menuUrl]
        mobileDisplayOrder <- map[APIKey.mobileDisplayOrder]
        numberOfSiblings <- map[APIKey.numOfSiblings]
        templateType <- map["template_type"]
        subMenus <- map["child_menu"]
        newArticleCount <- map["new_article_count"]
        listType <- map["list_type"]
    }
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...