2016-10-10 59 views
1

请帮忙!我试图用jackson kotlin模块从JSON生成对象。这里是json源代码:Kotlin Jackson从JSON生成对象

{ 
    "name": "row", 
    "type": "layout", 
    "subviews": [{ 
     "type": "horizontal", 
     "subviews": [{ 
      "type": "image", 
      "icon": "ic_no_photo", 
      "styles": { 
       "view": { 
        "gravity": "center" 
       } 
      } 
     }, { 
      "type": "vertical", 
      "subviews": [{ 
       "type": "text", 
       "fields": { 
        "text": "Some text 1" 
       } 
      }, { 
       "type": "text", 
       "fields": { 
        "text": "Some text 2" 
       } 
      }] 
     }, { 
      "type": "vertical", 
      "subviews": [{ 
       "type": "text", 
       "fields": { 
        "text": "Some text 3" 
       } 
      }, { 
       "type": "text", 
       "fields": { 
        "text": "Some text 4" 
       } 
      }] 
     }, { 
      "type": "vertical", 
      "subviews": [{ 
       "type": "image", 
       "icon": "ic_no_photo" 
      }, { 
       "type": "text", 
       "fields": { 
        "text": "Some text 5" 
       } 
      }] 
     }] 
    }] 
} 

我想生成Skeleton类的实例。

data class Skeleton (val type : String, 
         val name: String, 
         val icon: String, 
         val fields: List<Field>, 
         val styles: Map<String, Map<String, Any>>, 
         val subviews : List<Skeleton>) 

data class Field (val type: String, val value: Any) 

正如你所看到的,骨架对象可以有其他的骨架对象中(以及这些对象可以有内部过其他骨骼对象),也骨架可以有现场的对象列表

val mapper = jacksonObjectMapper() 
val skeleton: Skeleton = mapper.readValue(File(file)) 

此代码与异常结束:

com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class com.uibuilder.controllers.parser.Skeleton] value failed (java.lang.IllegalArgumentException): Parameter specified as non-null is null: method com.uibuilder.controllers.parser.Skeleton.<init>, parameter name 
at [Source: docs\layout.txt; line: 14, column: 3] (through reference chain: com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0]->com.uibuilder.controllers.parser.Skeleton["subviews"]->java.util.ArrayList[0]) 

回答

4

有几个问题,我发现你的映射,以防止杰克逊读取从JSON值:

  • Skeleton类具有非空的构造函数参数(例如, val type: String,而不是String?),并且Jackson将null传递给它们,如果这些参数的值在JSON中丢失。这是什么原因导致你提到的异常:

    参数指定非空是空:方法com.uibuilder.controllers.parser.Skeleton.<init>,参数name

    为了避免它,你应该标记可能可能值遗漏的参数可为空(所有你的情况参数):

    data class Skeleton(val type: String?, 
            val name: String?, 
            val icon: String?, 
            val fields: List<Field>?, 
            val styles: Map<String, Map<String, Any>>?, 
            val subviews : List<Skeleton>?) 
    
  • fieldsSkeleton的类型为List<Field>,但在JSON它由单个对象表示,而不是由数组表示。该解决将是该fields参数类型更改为Field?

    data class Skeleton(... 
            val fields: Field?, 
            ...) 
    
  • 此外,Field类代码不匹配的JSON对象:

    "fields": { 
        "text": "Some text 1" 
    } 
    

    你应该改变Field类作为好,使之具有text属性:

    data class Field(val text: String) 
    

在我做出我列出的更改后,Jackson可以成功读取相关的JSON。


参见:"Null Safety" in Kotlin reference

+0

非常感谢!你帮了我! ) –