非常不确定如何使用moshi反序列化JSON嵌套对象

时间:2018-05-31 00:17:14

标签: android json deserialization moshi

使用moshi从这个GET调用反序列化有点问题

https://min-api.cryptocompare.com/data/pricemultifull?fsyms=007,1337&tsyms=EUR,USD 结果看起来像这样。

{
   "RAW":{
     //not used 
},
"DISPLAY":{
  "1337":{
     "EUR":{
    "FROMSYMBOL":"1337",
    "TOSYMBOL":"€",
     "MARKET":"CryptoCompare Index",
    "PRICE":"€ 0.0001115",
    "LASTUPDATE":"Just now",
    "LASTVOLUME":"1337 0",
    "LASTVOLUMETO":"€ 0",
    "LASTTRADEID":0,
    "VOLUMEDAY":"1337 0",
    "VOLUMEDAYTO":"€ 0",
    "VOLUME24HOUR":"1337 0",
    "VOLUME24HOURTO":"€ 0",
    "OPENDAY":"€ 0.0001018",
    "HIGHDAY":"€ 0.0001115",
    "LOWDAY":"€ 0.0001018",
    "OPEN24HOUR":"€ 0.0001115",
    "HIGH24HOUR":"€ 0.0001115",
    "LOW24HOUR":"€ 0.0001018",
    "LASTMARKET":"Yobit",
    "CHANGE24HOUR":"€ 0.0",
    "CHANGEPCT24HOUR":"0.00",
    "CHANGEDAY":"€ 0.0000097",
    "CHANGEPCTDAY":"9.52",
    "SUPPLY":"1337 28,988,904,177.4",
    "MKTCAP":"€ 3,232.91 K",
    "TOTALVOLUME24H":"1337 1,360.28 K",
    "TOTALVOLUME24HTO":"€ 151.70"
     },
      "USD":{
      "FROMSYMBOL":"1337",
    "TOSYMBOL":"$",
    "MARKET":"CryptoCompare Index",
    "PRICE":"$ 0.0001490",
    "LASTUPDATE":"4 hours ago",
    "LASTVOLUME":"1337 12,553.1",
    "LASTVOLUMETO":"$ 1.87",
    "LASTTRADEID":"200003428",
    "VOLUMEDAY":"1337 19,183.0",
    "VOLUMEDAYTO":"$ 2.81",
    "VOLUME24HOUR":"1337 28,256.0",
    "VOLUME24HOURTO":"$ 3.95",
    "OPENDAY":"$ 0.0001250",
    "HIGHDAY":"$ 0.0001490",
    "LOWDAY":"$ 0.0001250",
    "OPEN24HOUR":"$ 0.0001250",
    "HIGH24HOUR":"$ 0.0001490",
    "LOW24HOUR":"$ 0.0001250",
    "LASTMARKET":"Yobit",
    "CHANGE24HOUR":"$ 0.000024",
    "CHANGEPCT24HOUR":"19.20",
    "CHANGEDAY":"$ 0.000024",
    "CHANGEPCTDAY":"19.20",
    "SUPPLY":"1337 28,988,904,177.4",
    "MKTCAP":"$ 4,319.35 K",
    "TOTALVOLUME24H":"1337 1,360.28 K",
    "TOTALVOLUME24HTO":"$ 202.43"
 }
 },
 "007":{
 "EUR":{
    "FROMSYMBOL":"007",
    "TOSYMBOL":"€",
    "MARKET":"CryptoCompare Index",
    "PRICE":"€ 1.08",
    "LASTUPDATE":"Just now",
    "LASTVOLUME":"007 0",
    "LASTVOLUMETO":"€ 0",
    "LASTTRADEID":0,
    "VOLUMEDAY":"007 0",
    "VOLUMEDAYTO":"€ 0",
    "VOLUME24HOUR":"007 0",
    "VOLUME24HOURTO":"€ 0",
    "OPENDAY":"€ 1.08",
    "HIGHDAY":"€ 1.08",
    "LOWDAY":"€ 1.08",
    "OPEN24HOUR":"€ 1.08",
    "HIGH24HOUR":"€ 1.08",
    "LOW24HOUR":"€ 1.08",
    "LASTMARKET":"Yobit",
    "CHANGE24HOUR":"€ 0.0",
    "CHANGEPCT24HOUR":"0.00",
    "CHANGEDAY":"€ -0.00013",
    "CHANGEPCTDAY":"-0.01",
    "SUPPLY":"007 0",
    "MKTCAP":"€ 0",
    "TOTALVOLUME24H":"007 0",
    "TOTALVOLUME24HTO":"€ 0"
 },
     "USD":{
    "FROMSYMBOL":"007",
    "TOSYMBOL":"$",
    "MARKET":"CryptoCompare Index",
    "PRICE":"$ 6.51",
    "LASTUPDATE":"4 months ago",
    "LASTVOLUME":"007 0.01536",
    "LASTVOLUMETO":"$ 0.1000",
    "LASTTRADEID":"182056756",
    "VOLUMEDAY":"007 0",
    "VOLUMEDAYTO":"$ 0",
    "VOLUME24HOUR":"007 0",
    "VOLUME24HOURTO":"$ 0",
    "OPENDAY":"$ 6.51",
    "HIGHDAY":"$ 6.51",
    "LOWDAY":"$ 6.51",
    "OPEN24HOUR":"$ 6.51",
    "HIGH24HOUR":"$ 6.51",
    "LOW24HOUR":"$ 6.51",
    "LASTMARKET":"Yobit",
    "CHANGE24HOUR":"$ 0.0",
    "CHANGEPCT24HOUR":"0.00",
    "CHANGEDAY":"$ -0.00034",
    "CHANGEPCTDAY":"-0.01",
    "SUPPLY":"007 0",
    "MKTCAP":"$ 0",
    "TOTALVOLUME24H":"007 0",
    "TOTALVOLUME24HTO":"$ 0"
   }
  }
 }
}

收到以下错误。

com.squareup.moshi.JsonDataException: Required value 'displayMapToCurrency' 
missing at $.DISPLAY.1337

我使用以下数据类包装另一个

//外层

data class FullPriceWrapper(
@Json( name = "DISPLAY")
val DISPLAY : Map<String, FullPriceWrapperInternalDisplay>
)

//中级班

data class FullPriceWrapperInternalDisplay(
   val displayMapToCurrency : Map<String,CurrencyFullPriceDataDisplay>
)

//最内在的课程

data class CurrencyFullPriceDataDisplay(
@Json(name = "FROMSYMBOL")
val FROMSYMBOL: String,
@Json(name = "TOSYMBOL")
val TOSYMBOL: String,
@Json(name = "MARKET")
val MARKET: String)

所以我假设中间类没有在这里从错误消息中实例化? moshi中是否存在嵌套类的问题?我通过改进创建我的api实例,如下面传递的MoshiConverterFactory。

return Retrofit.Builder()
            .baseUrl(compareApiEndPoint)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build().create(CryptoApi::class.java)

我还尝试过代码生成注释,为每个类创建适配器,但没有任何乐趣。生成适配器。也许我有错误的想法,但我认为这些是用注释自动生成的? (如下所示)

@JsonClass(generateAdapter = true)

但我认为我目前对图书馆不够熟悉。我想知道是否需要添加自定义适配器,就像我在其他示例中看到的那样?我也试过这个,但我不确定如何从我见过的例子中创建这些适配器?如果有人有关于如何纠正这个错误的想法,这将是伟大的!我也希望这个问题相对准确。谢谢!

0 个答案:

没有答案