【问题标题】:how to extract Json data in AndroidStudio如何在 Android Studio 中提取 Json 数据
【发布时间】:2021-08-15 08:35:54
【问题描述】:

如何在我不知道这种情况下的参数的情况下提取Json数据

[
{
id: "binance",
name: "Binance",
year_established: 2017,
country: "Cayman Islands",
description: "",
url: "https://www.binance.com/",
image: "https://assets.coingecko.com/markets/images/52/small/binance.jpg?1519353250",
has_trading_incentive: false,
trust_score: 10,
trust_score_rank: 1,
trade_volume_24h_btc: 1101704.8108077163,
trade_volume_24h_btc_normalized: 1101704.8108077163
},]

虽然我创建了一个数据类,但我无法在 [ fun getExchanges(): Call ] 中使用它

package com.example.cryptotracker

data class Crypto (
    val name:String,
    val country:String,
    val url:String,
    val image:String,
    val trade_volume_24h_btc:String,
    val trade_volume_24h_btc_normalized:String

    )

这里我使用以下 Data 类在不同的 App 中提取 Json 数据。

package com.example.news2

 data class News (
     val totalResults:Int,
     val articles: List<Articles>
  )
package com.example.news2

data class Articles (

    val author:String,
    val title:String,
    val description:String,
    val url:String,
    val urlToImage:String

   )
{
status: "ok",
totalResults: 70,
-articles: [
{
source: {
id: null,
name: "CNBC"
},
author: "Eustance Huang",
title: "Electric vehicles need to be owned longer, driven further to offset 'embedded carbon,' Jefferies says - CNBC",
description: "Jefferies' Simon Powell says a "huge amount" of carbon is emitted when materials like steel and aluminum are created and put together for vehicle manufacturing.",
url: "https://www.cnbc.com/2021/05/27/jefferies-on-the-carbon-challenges-in-electric-vehicle-manufacturing.html",
urlToImage: "https://image.cnbcfm.com/api/v1/image/106880053-1620412523089-gettyimages-1232464403-EVGo_005_04222021JPG.jpeg?v=1622013741",
publishedAt: "2021-05-27T01:16:00Z",
content: "Electric vehicle manufacturing currently faces an "embedded carbon" challenge, says Jefferies' Simon Powell. "To gain the environmental dividend that governments are looking for, users are going to … [+2028 chars]"
} ]

【问题讨论】:

  • 您的具体要求是什么?你想解析json?
  • 是的!我要解析Json数组?

标签: android json android-studio kotlin


【解决方案1】:

如果您需要以 JSON 格式解码 Cryptos 对象数组,您可以使用 Gson 库:

val cryptoJsonArray = [
    {
        id: "binance",
        name: "Binance",
        year_established: 2017,
        country: "Cayman Islands",
        description: "",
        url: "https://www.binance.com/",
        image: "https://assets.coingecko.com/markets/images/52/small/binance.jpg?1519353250",
        has_trading_incentive: false,
        trust_score: 10,
        trust_score_rank: 1,
        trade_volume_24h_btc: 1101704.8108077163,
        trade_volume_24h_btc_normalized: 1101704.8108077163
    }
]

val cryptoArrayType = object : TypeToken<List<Crypto>>() {}.type
val cryptoList = Gson().fromJson<List<Crypto>>(cryptoJsonArray, cryptoArrayType)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多