gpt4 book ai didi

java - Java中解析JSON问题

转载 作者:行者123 更新时间:2023-12-02 02:00:42 25 4
gpt4 key购买 nike

抱歉,我对用 java 解析 JSON 很陌生。

我编写了一个循环,从一副 52 张牌中挑选 5 张牌(见下文)。挑选完卡片后,我想验证是否还有 52 张卡片。

在 API 上,它确认还剩下多少张牌,但我似乎找不到一个值来告诉我已选择了多少张牌。

https://deckofcardsapi.com/api/deck/new/draw/?count=3

-我开始编写下面的代码来检索剩余卡数的数据,但它似乎不起作用。

JSONObject obj = new JSONObject("");
String pageName = obj.getJSONObject("Remaining").getString("Value");
System.out.println("remaining + value");

下面是要解析的JSON

{"remaining": 49, "deck_id": "nzzp9cwqokp6", "cards": [{"code": "AS", "images": {"png": "https://deckofcardsapi.com/static/img/AS.png", "svg": "https://deckofcardsapi.com/static/img/AS.svg"}, "value": "ACE", "image": "https://deckofcardsapi.com/static/img/AS.png", "suit": "SPADES"}, {"code": "2S", "images": {"png": "https://deckofcardsapi.com/static/img/2S.png", "svg": "https://deckofcardsapi.com/static/img/2S.svg"}, "value": "2", "image": "https://deckofcardsapi.com/static/img/2S.png", "suit": "SPADES"}, {"code": "6H", "images": {"png": "https://deckofcardsapi.com/static/img/6H.png", "svg": "https://deckofcardsapi.com/static/img/6H.svg"}, "value": "6", "image": "https://deckofcardsapi.com/static/img/6H.png", "suit": "HEARTS"}], "success": true}

-

    int counter = 5;
for(int i = 1; i <= counter; i++) { //5 inclusive otherwise use <
String uriString = "https://deckofcardsapi.com/api/deck/new/draw/?count="+i;
System.out.println(i);

最佳答案

现有:

JSONObject obj = new JSONObject("");
String pageName = obj.getJSONObject("Remaining").getString("Value");
System.out.println("remaining + value");

根据您的 json 响应,“值”是卡片对象的一部分。要访问值,您需要访问第一个卡片对象,然后您可以从中提取“值”。

要获取值(value),应该是这样的:

public static void getResponse() throws JSONException {
String response = "{\"remaining\": 49, \"deck_id\": \"nzzp9cwqokp6\", \"cards\": [{\"code\": \"AS\", \"images\": {\"png\": \"https://deckofcardsapi.com/static/img/AS.png\", \"svg\": \"https://deckofcardsapi.com/static/img/AS.svg\"}, \"value\": \"ACE\", \"image\": \"https://deckofcardsapi.com/static/img/AS.png\", \"suit\": \"SPADES\"}, {\"code\": \"2S\", \"images\": {\"png\": \"https://deckofcardsapi.com/static/img/2S.png\", \"svg\": \"https://deckofcardsapi.com/static/img/2S.svg\"}, \"value\": \"2\", \"image\": \"https://deckofcardsapi.com/static/img/2S.png\", \"suit\": \"SPADES\"}, {\"code\": \"6H\", \"images\": {\"png\": \"https://deckofcardsapi.com/static/img/6H.png\", \"svg\": \"https://deckofcardsapi.com/static/img/6H.svg\"}, \"value\": \"6\", \"image\": \"https://deckofcardsapi.com/static/img/6H.png\", \"suit\": \"HEARTS\"}], \"success\": true}";

JSONObject responseObj = new JSONObject( response);
JSONArray contracts = (JSONArray) responseObj.get("cards");
JSONObject cards = (JSONObject)contracts.get(0);
System.out.println(cards.get("value"));
}

enter image description here

关于java - Java中解析JSON问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51630158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com