This is my JSON file: http://ddragon.leagueoflegends.com/cdn/9.22.1/data/en_US/champion.json
The JSON has an ID and Name as two keys along with some other keys. For example,
{
"type": "champion",
"format": "standAloneComplex",
"version": "9.22.1",
"data": {
"Aatrox": {
"version": "9.22.1",
"id": "Aatrox",
"key": "266",
"name": "Aatrox",
"title": "the Darkin Blade"
}
}
}
So for a given key, say 'key=266', I want the id="Aatrox".
I found this code:
public String loadJSONFromAsset(Context context) {
String json = null;
try {
InputStream is = context.getAssets().open("champion.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
and im trying this:
JSONObject obj = new JSONObject(loadJSONFromAsset(context));
JSONArray m_jArry = obj.getJSONArray("data");
JSONObject jo_inside = m_jArry.getJSONObject(key);
String champName = jo_inside.getString("id");
EDITED This is my class where i already have my "key":
public class MatchListAdapter extends ArrayAdapter<Match> {
private static final String TAG = "MatchListAdapter";
private Context mContext;
private int mResource;
private int lastPosition = -1;
String championName;
public MatchListAdapter(Context context, int resource, ArrayList<Match> objects){
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Get the champions information
String gameType = getItem(position).getGameType();
int key = getItem(position).getKEY();
String sex = getItem(position).getSex();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView tvName = convertView.findViewById(R.id.testName);
TextView tvBirthday = convertView.findViewById(R.id.textView2);
TextView tvSex = convertView.findViewById(R.id.textView3);
ImageView championIMG = convertView.findViewById(R.id.championImage);
//Get name from champion.json using my "key" and then load imgage using Picasso
Picasso.get()
.load("http://ddragon.leagueoflegends.com/cdn/9.22.1/img/champion/"+championName+".png")
.resize(100,100)
.placeholder(R.drawable.ic_launcher_background)
.into(championIMG, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Log.d("error", "error Message: "+e.getMessage());
}
});
tvName.setText(gameType);
tvBirthday.setText("id: " + key);
tvSex.setText(sex);
return convertView;
}
}