-1

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.

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
1haker
  • 124
  • 13
  • Okay i understand that, but how change findFirst/findAll methods ? – 1haker Nov 22 '19 at 12:05
  • They are entirely different JSON structures. `queues.json` is an array of simple objects (4 fields), while `champion.json` is a large complex set of nested objects. They have nothing in common, so don't try to process them the same way. They are not compatible with each other. Write *different* code for them. – Andreas Nov 22 '19 at 12:10

1 Answers1

0

The Problem is champions.json is type of JSONObject but the queue.json is JSONArray , so there is jsonobject in each position

So for that , get the resuly as JSONArray and the add one for loop like this,

for (int i = 0;i<json.length();i++){
  JSONObject jsonObj = json.getJSONObject(i);
  // get data from jsonobject
}
Peter Alwin
  • 239
  • 1
  • 6
  • Thank you :) Someone maybe need this too: https://www.oodlestechnologies.com/blogs/Reading-JSON-file-in-android/ – 1haker Nov 22 '19 at 13:21