I want to create a YuGiOh! game software. So, the first step for me was collecting all cards to make a card search engine.
So I called an API to get cards data, and the issue I don't to how from it map them into card class and add them to a card list the will be displayed to the user who will select the ones he needs.
THANK YOU FOR YOUR HELP
package card;
import java.util.List;
public class card {
private int atk;
private int def;
private int level;
private String race;
private String attribute;
private String archetype;
private String imagePath;
private String name;
card(int atk, int def, int level, String name, String attribute, String imagePath)
{
this.atk = atk;
this.def = def;
this.level = level;
this.attribute = attribute;
this.imagePath = imagePath;
this.name = name;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getName() {
return name;
}
public void setName(String imagePath) {
this.name= name;
}
}
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import card.card;
public class API {
public void searchCard()
{
try
{
URL url = new URL("https://db.ygoprodeck.com/api/v7/cardinfo.php?name=Dark%");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
//Check if connect is made
int responseCode = conn.getResponseCode();
// 200 OK
if (responseCode != 200)
{
throw new RuntimeException("HttpResponseCode: " + responseCode);
}
else
{
ObjectMapper mapper = new ObjectMapper();
JsonNode cardData = mapper.readTree(url.openStream());
card[] cardList = mapper.readValue(url.openStream(), card[].class);
System.out.print(cardList[0].getName());
//return cardData;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Lcard.card;` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1746)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1520)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1467)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:345)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:196)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:26)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4697)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3689)
at API.API.searchCard(API.java:46)
at APP.app.main(app.java:9)
Here is an example of data collected from the api.
{"data":[{"id":55289183,"name":"Capricious Darklord","type":"Effect Monster","desc":"During the Main Phase (Quick Effect): You can activate this effect; Tribute Summon 1 Fairy monster face-up. If this card is sent to the GY: You can make all monsters your opponent currently controls lose 500 ATK/DEF for each Fairy monster on the field, until the end of this turn. You can only use each effect of \"Capricious Darklord\" once per turn.","atk":0,"def":1600,"level":4,"race":"Fairy","attribute":"DARK","archetype":"Darklord","card_sets":[{"set_name":"2021 Tin of Ancient Battles","set_code":"MP21-EN117","set_rarity":"Common","set_rarity_code":"(C)","set_price":"0.88"},{"set_name":"Rise of the Duelist","set_code":"ROTD-EN023","set_rarity":"Common","set_rarity_code":"(C)","set_price":"0.99"}],"card_images":[{"id":55289183,"image_url":"https://storage.googleapis.com/ygoprodeck.com/pics/55289183.jpg","image_url_small":"https://storage.googleapis.com/ygoprodeck.com/pics_small/55289183.jpg"}],"card_prices":[{"cardmarket_price":"0.08","tcgplayer_price":"0.08","ebay_price":"0.99","amazon_price":"0.25","coolstuffinc_price":"0.49"}]},{"id":85325774,"name":"Charge Into a Dark World","type":"Spell Card","desc":"Target 1 Level 4 or lower Fiend monster in your GY; Special Summon it, then discard 1 Fiend monster. You can only activate 1 \"Charge Into a Dark World\" per turn.","race":"Normal","archetype":"Dark World","card_sets":[{"set_name":"2021 Tin of Ancient Battles","set_code":"MP21-EN206","set_rarity":"Common","set_rarity_code":"(C)","set_price":"0.96"},{"set_name":"Phantom Rage","set_code":"PHRA-EN063","set_rarity":"Common","set_rarity_code":"(C)","set_price":"0.99"}],"card_images":...