【问题标题】:Not a JSON Object Excpetion in one file but diffrent json file works不是一个文件中的 JSON 对象异常,但不同的 json 文件有效
【发布时间】:2019-11-22 11:49:15
【问题描述】:

我有 2 个 .json 文件,我使用 key/id 从中获取数据。对于this one,一切正常,但是当我尝试使用that one

得到错误:

IllegalStateException:不是 JSON 对象:[{"queueId":0,"map":"Custom games", . . .

这是我适用于 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;
}

}

另外,我找到了这个解决方案:java.lang.IllegalStateException: Not a JSON Object,但我不知道如何为我的代码调整它。

【问题讨论】:

  • 好的,我明白了,但是如何更改 findFirst/findAll 方法?
  • 它们是完全不同的 JSON 结构。 queues.json 是一个简单对象数组(4 个字段),而champion.json 是一个大型复杂嵌套对象集。它们没有任何共同点,所以不要试图以同样的方式处理它们。它们彼此不兼容。为他们编写不同的代码。

标签: java android json


【解决方案1】:

问题是 Champions.json 是 JSONObject 的类型 但是 queue.json 是 JSONArray ,所以每个位置都有 jsonobject

因此,获取结果为 JSONArray 并像这样添加一个 for 循环,

for (int i = 0;i<json.length();i++){
  JSONObject jsonObj = json.getJSONObject(i);
  // get data from jsonobject
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多