JAVA如何将JSONObject转换成自定义格式?

JAVA how to convert JSONObject into a custom format?

我想做的是把某个JSON文件改成自定义格式,找了两天资料一直没弄明白也没有有人问这个....

格式化前

{
   "book": [

      {
         "id": "01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },

      {
         "id": "07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }

   ]
}

正在尝试将其格式化为:

Book
      Id : 01
      Language : Java
      ...

      Id : 07
      Language : C++

实际JSON数据是这样的:

[
    {
        "symbol": "BTC",
        "circulating_supply": 18729837,
        "last_updated": "2021-06-08T10:17:02.000Z",
        "total_supply": 18729837,
        "cmc_rank": 1,
        "platform": null,
        "tags": [
            "mineable",
            "pow",
            "sha-256",
            "store-of-value",
            "state-channels",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "binance-labs-portfolio",
            "arrington-xrp-capital",
            "blockchain-capital-portfolio",
            "boostvc-portfolio",
            "cms-holdings-portfolio",
            "dcg-portfolio",
            "dragonfly-capital-portfolio",
            "electric-capital-portfolio",
            "fabric-ventures-portfolio",
            "framework-ventures",
            "galaxy-digital-portfolio",
            "huobi-capital",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "1confirmation-portfolio",
            "winklevoss-capital",
            "usv-portfolio",
            "placeholder-ventures-portfolio",
            "pantera-capital-portfolio",
            "multicoin-capital-portfolio",
            "paradigm-xzy-screener"
        ],
        "date_added": "2013-04-28T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -42.63012856,
            "percent_change_1h": 0.38317977,
            "last_updated": "2021-06-08T10:17:02.000Z",
            "percent_change_24h": -9.36685756,
            "market_cap": 619325019564.578,
            "price": 33066.22580669431,
            "percent_change_60d": -43.54639481,
            "volume_24h": 43132365330.36409,
            "percent_change_90d": -39.86303315,
            "percent_change_7d": -9.21607228
        }},
        "num_market_pairs": 9722,
        "name": "Bitcoin",
        "max_supply": 21000000,
        "id": 1,
        "slug": "bitcoin"
    },
    {
        "symbol": "ETH",
        "circulating_supply": 116205430.624,
        "last_updated": "2021-06-08T10:17:02.000Z",
        "total_supply": 116205430.624,
        "cmc_rank": 2,
        "platform": null,
        "tags": [
            "mineable",
            "pow",
            "smart-contracts",
            "ethereum",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "binance-labs-portfolio",
            "arrington-xrp-capital",
            "blockchain-capital-portfolio",
            "boostvc-portfolio",
            "cms-holdings-portfolio",
            "dcg-portfolio",
            "dragonfly-capital-portfolio",
            "electric-capital-portfolio",
            "fabric-ventures-portfolio",
            "framework-ventures",
            "hashkey-capital-portfolio",
            "kinetic-capital",
            "huobi-capital",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "1confirmation-portfolio",
            "winklevoss-capital",
            "usv-portfolio",
            "placeholder-ventures-portfolio",
            "pantera-capital-portfolio",
            "multicoin-capital-portfolio",
            "paradigm-xzy-screener"
        ],
        "date_added": "2015-08-07T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -34.93326351,
            "percent_change_1h": 0.0977492,
            "last_updated": "2021-06-08T10:17:02.000Z",
            "percent_change_24h": -10.67415695,
            "market_cap": 292465567141.6079,
            "price": 2516.797756964766,
            "percent_change_60d": 20.00514111,
            "volume_24h": 38463882117.94565,
            "percent_change_90d": 37.73403958,
            "percent_change_7d": -3.05561192
        }},
        "num_market_pairs": 6053,
        "name": "Ethereum",
        "max_supply": null,
        "id": 1027,
        "slug": "ethereum"
    },
    {
        "symbol": "USDT",
        "circulating_supply": 62280355385.45168,
        "last_updated": "2021-06-08T10:16:08.000Z",
        "total_supply": 64471767616.826355,
        "cmc_rank": 3,
        "platform": {
            "symbol": "ETH",
            "name": "Ethereum",
            "token_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
            "id": 1027,
            "slug": "ethereum"
        },
        "tags": [
            "store-of-value",
            "payments",
            "stablecoin",
            "stablecoin-asset-backed",
            "solana-ecosystem"
        ],
        "date_added": "2015-02-25T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": 0.08689942,
            "percent_change_1h": -0.01514305,
            "last_updated": "2021-06-08T10:16:08.000Z",
            "percent_change_24h": 0.00252658,
            "market_cap": 62333503909.90704,
            "price": 1.00085337542033,
            "percent_change_60d": 0.00890571,
            "volume_24h": 88090197454.22122,
            "percent_change_90d": 0.02421796,
            "percent_change_7d": 0.03190558
        }},
        "num_market_pairs": 13483,
        "name": "Tether",
        "max_supply": null,
        "id": 825,
        "slug": "tether"
    },
    {
        "symbol": "BNB",
        "circulating_supply": 153432897,
        "last_updated": "2021-06-08T10:17:08.000Z",
        "total_supply": 169432897,
        "cmc_rank": 4,
        "platform": null,
        "tags": [
            "marketplace",
            "centralized-exchange",
            "payments",
            "binance-smart-chain",
            "alameda-research-portfolio",
            "multicoin-capital-portfolio"
        ],
        "date_added": "2017-07-25T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -46.98637267,
            "percent_change_1h": -0.06201527,
            "last_updated": "2021-06-08T10:17:08.000Z",
            "percent_change_24h": -12.9766759,
            "market_cap": 54032968135.19999,
            "price": 352.1602550149333,
            "percent_change_60d": -18.72766515,
            "volume_24h": 3975040241.706507,
            "percent_change_90d": 24.3131346,
            "percent_change_7d": 0.46465171
        }},
        "num_market_pairs": 619,
        "name": "Binance Coin",
        "max_supply": 170532785,
        "id": 1839,
        "slug": "binance-coin"
    },
    {
        "symbol": "ADA",
        "circulating_supply": 31930548128.29,
        "last_updated": "2021-06-08T10:16:09.000Z",
        "total_supply": 32681678108.601,
        "cmc_rank": 5,
        "platform": null,
        "tags": [
            "mineable",
            "dpos",
            "pos",
            "platform",
            "research",
            "smart-contracts",
            "staking",
            "binance-chain"
        ],
        "date_added": "2017-10-01T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -9.47981292,
            "percent_change_1h": -1.0109768,
            "last_updated": "2021-06-08T10:16:09.000Z",
            "percent_change_24h": -10.68780802,
            "market_cap": 49082262157.72255,
            "price": 1.53715689315826,
            "percent_change_60d": 26.8779134,
            "volume_24h": 3602351530.9447293,
            "percent_change_90d": 31.00161031,
            "percent_change_7d": -9.66192067
        }},
        "num_market_pairs": 276,
        "name": "Cardano",
        "max_supply": 45000000000,
        "id": 2010,
        "slug": "cardano"
    },
    {
        "symbol": "DOGE",
        "circulating_supply": 129939361256.40326,
        "last_updated": "2021-06-08T10:17:03.000Z",
        "total_supply": 129939361256.40326,
        "cmc_rank": 6,
        "platform": null,
        "tags": [
            "mineable",
            "pow",
            "scrypt",
            "medium-of-exchange",
            "memes",
            "payments"
        ],
        "date_added": "2013-12-15T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -27.85005369,
            "percent_change_1h": -0.39664229,
            "last_updated": "2021-06-08T10:17:03.000Z",
            "percent_change_24h": -12.06689716,
            "market_cap": 42746878864.849754,
            "price": 0.32897559639761,
            "percent_change_60d": 425.01053128,
            "volume_24h": 4256466078.3377676,
            "percent_change_90d": 479.75690549,
            "percent_change_7d": 1.48509162
        }},
        "num_market_pairs": 376,
        "name": "Dogecoin",
        "max_supply": null,
        "id": 74,
        "slug": "dogecoin"
    },
    {
        "symbol": "XRP",
        "circulating_supply": 46151013329,
        "last_updated": "2021-06-08T10:17:03.000Z",
        "total_supply": 99990399027,
        "cmc_rank": 7,
        "platform": null,
        "tags": [
            "medium-of-exchange",
            "enterprise-solutions",
            "binance-chain",
            "arrington-xrp-capital",
            "galaxy-digital-portfolio",
            "a16z-portfolio",
            "pantera-capital-portfolio"
        ],
        "date_added": "2013-08-04T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -42.92640139,
            "percent_change_1h": -0.60106842,
            "last_updated": "2021-06-08T10:17:03.000Z",
            "percent_change_24h": -10.12249253,
            "market_cap": 39897934998.12378,
            "price": 0.8645083199734,
            "percent_change_60d": -15.63161843,
            "volume_24h": 4179902561.947919,
            "percent_change_90d": 84.29141004,
            "percent_change_7d": -14.85218801
        }},
        "num_market_pairs": 682,
        "name": "XRP",
        "max_supply": 100000000000,
        "id": 52,
        "slug": "xrp"
    },
    {
        "symbol": "USDC",
        "circulating_supply": 23075185630.71199,
        "last_updated": "2021-06-08T10:17:04.000Z",
        "total_supply": 23075185630.71199,
        "cmc_rank": 8,
        "platform": {
            "symbol": "ETH",
            "name": "Ethereum",
            "token_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
            "id": 1027,
            "slug": "ethereum"
        },
        "tags": [
            "medium-of-exchange",
            "stablecoin",
            "stablecoin-asset-backed"
        ],
        "date_added": "2018-10-08T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": 0.05408204,
            "percent_change_1h": -0.00816775,
            "last_updated": "2021-06-08T10:17:04.000Z",
            "percent_change_24h": 0.00649147,
            "market_cap": 23084217521.810787,
            "price": 1.00039141141672,
            "percent_change_60d": -0.05832072,
            "volume_24h": 2455031484.812885,
            "percent_change_90d": 0.02446002,
            "percent_change_7d": -0.0056865
        }},
        "num_market_pairs": 1068,
        "name": "USD Coin",
        "max_supply": null,
        "id": 3408,
        "slug": "usd-coin"
    },
    {
        "symbol": "DOT",
        "circulating_supply": 945735752.6497443,
        "last_updated": "2021-06-08T10:17:05.000Z",
        "total_supply": 1081259867.5039322,
        "cmc_rank": 9,
        "platform": null,
        "tags": [
            "substrate",
            "polkadot",
            "binance-chain",
            "polkadot-ecosystem",
            "three-arrows-capital-portfolio",
            "polychain-capital-portfolio",
            "blockchain-capital-portfolio",
            "boostvc-portfolio",
            "cms-holdings-portfolio",
            "coinfund-portfolio",
            "fabric-ventures-portfolio",
            "fenbushi-capital-portfolio",
            "hashkey-capital-portfolio",
            "kinetic-capital",
            "1confirmation-portfolio",
            "placeholder-ventures-portfolio",
            "pantera-capital-portfolio",
            "exnetwork-capital-portfolio"
        ],
        "date_added": "2020-08-19T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -45.72649058,
            "percent_change_1h": -0.3486271,
            "last_updated": "2021-06-08T10:17:05.000Z",
            "percent_change_24h": -15.05195784,
            "market_cap": 20076748179.497543,
            "price": 21.22870804370766,
            "percent_change_60d": -48.77804479,
            "volume_24h": 2421158822.0532265,
            "percent_change_90d": -44.73258504,
            "percent_change_7d": -5.5027651
        }},
        "num_market_pairs": 202,
        "name": "Polkadot",
        "max_supply": null,
        "id": 6636,
        "slug": "polkadot-new"
    },
    {
        "symbol": "UNI",
        "circulating_supply": 575107764.052485,
        "last_updated": "2021-06-08T10:16:11.000Z",
        "total_supply": 1000000000,
        "cmc_rank": 10,
        "platform": {
            "symbol": "ETH",
            "name": "Ethereum",
            "token_address": "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984",
            "id": 1027,
            "slug": "ethereum"
        },
        "tags": [
            "decentralized-exchange",
            "defi",
            "dao",
            "yield-farming",
            "amm",
            "coinbase-ventures-portfolio",
            "three-arrows-capital-portfolio",
            "governance",
            "blockchain-capital-portfolio",
            "defiance-capital",
            "alameda-research-portfolio",
            "a16z-portfolio",
            "pantera-capital-portfolio",
            "parafi-capital",
            "paradigm-xzy-screener"
        ],
        "date_added": "2020-09-17T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": -40.90238231,
            "percent_change_1h": -0.57439386,
            "last_updated": "2021-06-08T10:16:11.000Z",
            "percent_change_24h": -11.84522167,
            "market_cap": 13448377375.304375,
            "price": 23.38409984337659,
            "percent_change_60d": -23.21587997,
            "volume_24h": 569769688.2472888,
            "percent_change_90d": -26.98611642,
            "percent_change_7d": -13.63660968
        }},
        "num_market_pairs": 270,
        "name": "Uniswap",
        "max_supply": 1000000000,
        "id": 7083,
        "slug": "uniswap"
    },
    {
        "symbol": "ICP",
        "circulating_supply": 124103517.19,
        "last_updated": "2021-06-08T10:17:07.000Z",
        "total_supply": 469293126.8967923,
        "cmc_rank": 11,
        "platform": null,
        "tags": [
            "platform",
            "distributed-computing",
            "polychain-capital-portfolio",
            "exnetwork-capital-portfolio"
        ],
        "date_added": "2021-03-23T00:00:00.000Z",
        "quote": {"USD": {
            "percent_change_30d": 0,
            "percent_change_1h": -0.08952838,
            "last_updated": "2021-06-08T10:17:07.000Z",
            "percent_change_24h": -15.3124386,
            "market_cap": 10992912823.823347,
            "price": 88.57857595601757,
            "percent_change_60d": 0,
            "volume_24h": 260287850.43941185,
            "percent_change_90d": 0,
            "percent_change_7d": -20.06340267
        }},
        "num_market_pairs": 33,
        "name": "Internet Computer",
        "max_supply": null,
        "id": 8916,
        "slug": "internet-computer"
    },
... and so on
]

到目前为止,我一直在尝试将 JSONObject 转换为 HashMap:

HashMap<String, Object> rs = new ObjectMapper().readValue(json.toString(), HashMap.class);

但是我真的不知道现在该怎么办...

有人告诉我用Jackson or GSon or nested HashMap,但是不知道怎么解决...

谁能帮帮我?提前致谢..!

不要使用 HashMap。创建真实数据 类:

public class Library {
    private Book[] book = { };

    public Book[] getBook() {
        return book;
    }

    public void setBook(Book[] books) {
        this.book = books;
    }
}

public class Book {
    private String id;
    private String language;
    private String edition;
    private String author;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getEdition() {
        return edition;
    }

    public void setEdition(String edition) {
        this.edition = edition;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

现在您可以传递 Library.class 而不是 HashMap.class。