[英]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.