몇가지 방법이 있습니다.
명명은 올려주신 JSON의 name을 따라가겠습니다.
첫째, JSONObject를 이용하는 방법입니다.
Value클래스를 만듭니다.
멤버로는 level과 id가 있습니다.
public class Value {
private int level;
private String id;
public Value(int level, String id) {
super();
this.level = level;
this.id = id;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
이제 parse하는 메서드를 만듭니다.
List<Value> parsePlainJson(String json) throws JSONException {
JSONArray ja = new JSONObject(json).getJSONArray("value");
if (ja == null) {
return null;
}
List<Value> list = new LinkedList<Value>();
int size = ja.length();
JSONObject jo;
for (int i = 0; i < size; i++) {
jo = ja.getJSONObject(i);
if (jo != null) {
list.add(new Value(jo.optInt("level"), jo.optString("id")));
}
}
return list;
}
두번째로는 Gson같은 라이브러리를 이용하는 방법입니다.
Gson라이브러리를 libs에 복사합니다.
ADT가 자동으로 다 빌드패스 잡아주겠죠...
이제 Value라는 값을 가지는 클래스를 만듭니다.
public class Response{
private List<Value> value;
public List<Value> getValue() {
return value;
}
}
Gson으로 parse합니다.
List<Value> parsePlainJson(String json) {
Response response = new Gson().fromJson(json, Response.class);
return response.getValue();
}
단순히 level값과 id값을 다른 배열에 넣고 싶다면 이렇게 하면 되겠죠.
private int[] levels;
private String[] ids;
void parsePlainJson(String json) throws JSONException {
JSONArray ja = new JSONObject(json).getJSONArray("value");
int size = ja.length();
levels = new int[size];
ids = new String[size];
JSONObject jo;
for (int i = 0; i < size; i++) {
jo = ja.getJSONObject(i);
levels[i] = jo.optInt("level");
ids[i] = jo.optString("id");
}
}