JsonObject到Json记录-格式输出

时间:2019-01-21 17:19:00

标签: java json scala gson jsonresponse

我正在使用CryptoCompare API提取加密符号详细信息;输出是 就像下面的带有嵌套JSON一样-我需要转换为以下格式的重新记录:

{ "ETH":{ "USD":{ "FROMSYMBOL":"Ξ", "TOSYMBOL":"$", "MARKET":"CryptoCompare Index", "PRICE":"$ 117.74", "LASTUPDATE":"Just now", "LASTVOLUME":"Ξ 0.01000", "LASTVOLUMETO":"$ 1.17", "LASTTRADEID":"44473885", "VOLUMEDAY":"Ξ 340,510.0", "VOLUMEDAYTO":"$ 39,874,960.0", "VOLUME24HOUR":"Ξ 418,836.6", "VOLUME24HOURTO":"$ 49,126,029.4", "OPENDAY":"$ 118.40", "HIGHDAY":"$ 119.29", "LOWDAY":"$ 114.48", "OPEN24HOUR":"$ 117.99", "HIGH24HOUR":"$ 119.50", "LOW24HOUR":"$ 114.12" } } }

我需要生成以下输出-每个符号的单独记录: 映射-每个货币NODE-作为“符号”字段添加,例如“ ETH”节点现在是“ Sym”:“ ETH”; 其余字段从最内层节点“ USD”直接移动

{ "Sym":"ETH", "PRICE":"$ 117.74", "LASTTRADEID":"44473885", "VOLUMEDAY":"Ξ 340,510.0", "VOLUMEDAYTO":"$ 39,874,960.0", "VOLUME24HOUR":"Ξ 418,836.6" }

正在使用的代码:

import com.crypto.cryptocompare.api.CryptoCompareApi
import com.google.gson.Gson
import com.google.gson.GsonBuilder;

object cryptoComapreMultiSCryptoPriceGson extends App{

  val gson = new Gson()
  val api = new CryptoCompareApi();
  //val response = api.priceMulti("ETH,DASH","BTC,USD,EUR", new Nothing() {})

  val m = new java.util.LinkedHashMap[String,Object]
  m.put("extraParams", "TestProject")

  val response = api.priceMultiFull(  //to get priceMultiFull
    "ETH,DASH,BTC",
    "USD",
    m)

    //val jsonRec = gson.toJsonTree(response)
    println(response.get("DISPLAY"))

}

有任何指针或帮助吗?

0 个答案:

没有答案