【问题标题】:How to get non null values of an object如何获取对象的非空值
【发布时间】:2021-07-26 23:42:26
【问题描述】:

我需要创建一个我不知道的对象 json 2 键。

我目前的结构是:

{
[  {id: obj1Id , property2: [{element1}, {element2}]},
   {id: obj2Id , property2: [{element1}, {element2}]},
   {propertyName3: propertyValue3 },
   {propertyName4: propertyValue4 }
]

}

我需要把它转换成这种形式的json:

  { obj1Id: {property2: [{element1}, {element2}]}},
    obj2Id: {property2: [{element1}, {element2}]}},
    propertyName3: propertyValue3,
    propertyName4: propertyValue4
  }

有没有方便的方法来实现,不检查propertyName3是否存在,propertyName4是否存在于原始结构中?

我不知道“obj1Id”和“obj2Id”的实际字符串是什么,我也不在乎。所以不幸的是它需要是一个哈希图,而不是一个特定的对象

【问题讨论】:

    标签: kotlin gson


    【解决方案1】:

    您可以创建一个新结构(在这种情况下,我们将调用 result)并将其传递给 Gson 序列化:

    fun main() {
        val structure: Set<List<Map<String, Any>>> = setOf(
            listOf(
                mapOf("id" to "obj1Id", "property2" to listOf(setOf("element1"), setOf("element2"))),
                mapOf("id" to "obj2Id", "property2" to listOf(setOf("element1"), setOf("element2"))),
                mapOf("propertyName3" to "propertyValue3"),
                mapOf("propertyName4" to "propertyValue4"),
            )
        )
        println(structure)
        // Outputs [[{id=obj1Id, property2=[[element1], [element2]]}, {id=obj2Id, property2=[[element1], [element2]]}, {propertyName3=propertyValue3}, {propertyName4=propertyValue4}]]
        
        // Creates a hash map where we'll add the entries of the original structure
        val result: MutableMap<String, Any> = mutableMapOf()
    
        // Iterating over each list inside the first-nested level
        structure.forEach { list ->
            // Iterating over each map inside the second-nested level 
            list.forEach { map ->
                // Check if this map has an `id` key 
                // If it has, evaluate to its value, else to null
                val idValue = map["id"] as String?
    
                // If there is an `id` key
                if (idValue != null) {
                    // Add its value to its respective property
                    result[idKey] = map.filterKeys { key -> key != "id" }
                    // Or, if you need to add only the "property2" key:
                    // result[idKey] = mapOf("property2" to map["property2"])
    
                } else {
                    // Add all these entries to the result map
                    result.putAll(map)
                    // Or, if you need to add only the keys starting with "propertyName":
                    // map.filterKeys { key -> key.startsWith("propertyName") }.forEach(result::put)
                }
            }
        }
        println(result)
        // Outputs {obj1Id={property2=[[element1], [element2]]}, obj2Id={property2=[[element1], [element2]]}, propertyName3=propertyValue3, propertyName4=propertyValue4}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-03
      • 2011-06-16
      • 2020-01-02
      • 2015-08-09
      • 2021-06-01
      • 1970-01-01
      • 2011-05-14
      • 2014-06-17
      相关资源
      最近更新 更多