0

Sorry I'm very new to parse JSON in java.

I've written a loop that picks 5 cards from a deck of 52 cards (see below). After the cards have been picked i want to validate there is still 52 cards.

On the API it confirms how many cards are remaining but i can't seem to find a value which tells me how many cards have been picked.

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

- I've began to write the code below to retrieve the data of how many cards are remaining but it doesn't seem to work.

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

Below is the JSON to be parsed

{"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);
4
  • So i'm expecting the remaining cards to be added with the cards that have already been picked so it equals 52 but this part isn't working. I think the main issue is i'm only able to see the remaining cards and not the value of how many cards have been picked Commented Aug 1, 2018 at 10:03
  • Maybe as first step change this line System.out.println("remaining + value"); to System.out.println("remaining " + pageName);? Commented Aug 1, 2018 at 10:10
  • ALso please provide sample of your API JSON response to be parsed. Otherwise there is nothing to say about your question... Commented Aug 1, 2018 at 10:17
  • @Vadim please see the updated question Commented Aug 1, 2018 at 10:19

2 Answers 2

1

Existing:

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

According to your json response "value" is part of cards object. To access values you need to access first cards object then you can extract "value" out of it.

To access value it should be like :

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

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain what is happening in your code please
@Newbie-908 you have to extract array of cards from your response then only you can get card value. Last line directly maps your json response to Cards Object in java. Cards class should be created first.
Is there anyway to make the last line less complex and easy to read? also can i have a system.out.printin to print the number of cards?
@Newbie-908 I have updated my answer for your easy understanding.
1
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);
 }

2 Comments

can you explain a bit about your code and the way it's going to solve the problem?
API is returning the data of cards picked in the form of an array whose key is "cards". So, here I am calculating the size of cards array returns by API. So I think it will solve the problem of "On the API it confirms how many cards are remaining but i can't seem to find a value which tells me how many cards have been picked."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.