I have 2 .json files from which I'm getting data using key/id. For this one everything works fine, but when I'm trying to use that one
getting error:
IllegalStateException: Not a JSON Object: [{"queueId":0,"map":"Custom games", . . .
This is my code which works for champion.json:
public class MatchListAdapter extends ArrayAdapter<Match> {
private static final String TAG = "MatchListAdapter";
private Context mContext;
private int mResource;
private int lastPosition = -1;
private InputStream stream;
private JsonObject json;
private Map.Entry<String, JsonElement> found;
MatchListAdapter(Context context, int resource, ArrayList<Match> objects){
super(context, resource, objects);
mContext = context;
mResource = resource;
}
private static String getValueForKey(JsonElement data, String key) {
return data.getAsJsonObject().get(key).getAsString();
}
private static Map.Entry<String, JsonElement> findFirst(JsonObject json, String key, String value) {
return (Map.Entry<String, JsonElement>) findAll(json, key, value).iterator().next();
}
private static Set<Object> findAll(JsonObject json, final String key, final String value) {
return json.getAsJsonObject("data").entrySet().stream().filter(new Predicate<Map.Entry<String, JsonElement>>() {
@Override
public boolean test(Map.Entry<String, JsonElement> entry) {
return entry.getValue().getAsJsonObject().get(key).getAsString().equals(value);
}
}).collect(Collectors.toSet());
}
private void loadJson(String filename){
try {
stream = mContext.getAssets().open(filename);
} catch (IOException e) {
e.printStackTrace();
}
InputStreamReader reader = new InputStreamReader(stream);
json = JsonParser.parseReader(reader).getAsJsonObject();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Get the champion information
int champKey = getItem(position).getChampionKey();
int queueID = getItem(position).getQueueID();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
//Find views IDs
ImageView championIMG = convertView.findViewById(R.id.championImage);
TextView mapTypeTv = convertView.findViewById(R.id.matchType);
//Get name from champion.json using my "key" and then load imgage using Picasso
String championName;
String myKey = Integer.toString(champKey);
loadJson("champion.json");
found = findFirst(json, "key", myKey);
championName = getValueForKey(found.getValue(), "name");
//Get "map" from queues.json using "queueID" and then set TextView
String mapType;
String queID = Integer.toString(queueID);
loadJson("queues.json");
found = findFirst(json, "queueId", queID);
mapType = getValueForKey(found.getValue(), "map");
Picasso.get()
.load("http://ddragon.leagueoflegends.com/cdn/9.23.1/img/champion/"+championName+".png")
.resize(100,100)
.placeholder(R.drawable.ic_launcher_background)
.into(championIMG, new Callback() {...});
mapTypeTv.setText(mapType);
return convertView;
}
}
Also, I found this solution: java.lang.IllegalStateException: Not a JSON Object ,but I don't know how to adjust this for my code.