2

私は JSON の専門家ではないので、何かが明らかに不足しているかどうかはわかりません。しかし、私がやろうとしているのは、これを解析することです:

[{"name":"Djinnibone"},{"name":"Djinnibutt","changedToAt":1413217187000},{"name":"Djinnibone","changedToAt":1413217202000},{"name":"TEsty123","changedToAt":1423048173000},{"name":"Djinnibone","changedToAt":1423048202000}]

Djinnibone に続く残りの名前だけを取得したくありません。私がなんとか作成したのはこれです。それは正しい数の名前を与えます。しかし、それらはすべて null です。この場合、 null,null,null,null .

public String getHistory(UUID uuid) throws Exception {
    String history = "";
    HttpURLConnection connection = (HttpURLConnection) new URL("https://api.mojang.com/user/profiles/"+uuid.toString().replace("-", "")+"/names").openConnection();
    JSONArray response = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
    JSONObject jsonObject = new JSONObject();
    for(int index = 1; index < response.size(); index++) {
        jsonObject.get(response.get(index));
        String name = (String) jsonObject.get("name");
        if(index < response.size()) {
            history = history + name + ",";
        } else {
            history = history + name + ".";
        }
    }
    return history == "" ? history = "none." : history;
}

助けてくれてありがとう!

4

1 に答える 1

1

あなたはほとんどそこにJSONObjectいます.配列からそれぞれを取得していますが、それを正しく使用していません. JSONObject各オブジェクトを抽出して直接使用するには、次のようにコードを変更するだけで済みます。途中で作成する必要はありません。

public String getHistory(UUID uuid) throws Exception {
    String history = "";
    HttpURLConnection connection = (HttpURLConnection) new URL("https://api.mojang.com/user/profiles/"+uuid.toString().replace("-", "")+"/names").openConnection();
    JSONArray response = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
    for(int index = 1; index < response.size(); index++) {
        JSONObject jsonObject = response.get(index);
        String name = (String) jsonObject.get("name");
        if(index < response.size()) {
            history = history + name + ",";
        } else {
            history = history + name + ".";
        }
    }
    return history == "" ? history = "none." : history;
}
于 2015-10-24T03:52:23.090 に答える