繁体   English   中英

在Java问题中解析JSON

[英]Parse JSON in Java issue

抱歉,我在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响应,“值”是card对象的一部分。 要访问值,您需要访问第一个卡对象,然后可以从其中提取“值”。

要获得价值,它应该像:

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"));
}

在此处输入图片说明

public static void main(String[] args) throws JSONException, IOException {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet getRequest = new HttpGet("https://deckofcardsapi.com/api/deck/new/draw/?count=5");
    getRequest.addHeader("accept", "application/json");
    HttpResponse response = httpClient.execute(getRequest);

    String result = EntityUtils.toString(response.getEntity());
    JSONObject jsonResult = new JSONObject(result);

    int pickedCardsCount = jsonResult.getJSONArray("cards").length();
    int remainingCardsCount = jsonResult.getInt("remaining");
    int totalCardsCount = pickedCardsCount + remainingCardsCount;

    System.out.println("No of cards picked: " + pickedCardsCount);
    System.out.println("No of cards remaining: " + remainingCardsCount);
    System.out.println("Total Cards: " + totalCardsCount);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM