创建一个动态映射用于分析多个输入文件到一个科特林OBJ

问题描述 投票:0回答:1

比方说,我有一个可以有不同的按键名称相同数据领域的JSON数据

JSON A型

{ “宽度”:1024, “高度”:768, “颜色”: “#FF0000”}

也有一种映射JSON,给我什么属性应地图什么数据类的Json A型

{ “宽度”:瓦特, “高度”:h时, “色彩”: “颜色”}

JSON B型

{ “WidthOfobj”:1024, “heightOfobj”:768, “colorOfobj”: “#FF0000”}

JSON类型映射文档B

{ “widthOfobj”:W “heightOfobj”:H, “colorOfobj”: “颜色”}

在我的科特林数据类我已经OBJ定义为

data class dimension(W: int, H: int, Color: String)

现在我想无论是JSON A型或B与读取映射文档帮助映射到科特林数据类。我会如何做这样的事情呢?

这里是我的想法为清楚起见目的流

伪代码

 Data class dimmenison( W: int, H: int, Color:String)// already defined
//Read mapping document to map 
    val Map = readmappingfile(mappingfile)  //e. g  Width to W
                                      //      Height to H
Val input Data= readJson(input) //      { "width": 1024, "height": 768, "color": "#ff0000" }
Val show result = applytransformation( mappingfile, input)
// inside applytransformation Read the Map and data and serialize it to data class  
//oneway of just serializing  it would be using gson 
val result = gson.fromJson(input,dimmension::class.java) // this would work if input was in format ( w->v , h-> v , Color->v) 

//我如何进行改变重量转换成W I调用GSON代码前上方将其序列化到数据类。注意它需要足够动态的,所以我们不写了一个硬编码映射。

java kotlin kotlin-android-extensions
1个回答
0
投票

我想你使用自定义解串器GSON。

检查这个example

© www.soinside.com 2019 - 2024. All rights reserved.