GSON과 for 루프를 사용하여 Java에서 Json arraylist (하위)를 구문 분석하려고합니다. 는하지만 다음과 같은 오류가 무엇입니까 : 여기 JSONArray 배열 = 새 JSONArray (string_of_json_array);
for each not applicable to expression type
required:array or java.lang.iterable
found:String
이 오류를 표시하는 주요 자바 클래스입니다
try {
br = new BufferedReader(new FileReader("user.json"));
Tree result = gson.fromJson(br, Tree.class);
if (result !=null){
for (User t : result.getCategory()){ //ERROR IS HERE (squiggly red line)
System.out.println(t.getId() + "-" + t.getName() + "-" + t.getCategory() + "-" + t.getPercentage()); //ERROR IS ALSO HERE FOR EACH VARIABLES
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
이 트리 클래스입니다 :
private String category;
@SerializedName("children")
@Expose
private List<Child> children = new ArrayList<Child>();
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
사용자 클래스 :
private String id;
@SerializedName("processed_lang")
@Expose
private String processedLang;
@SerializedName("source")
@Expose
private String source;
@SerializedName("tree")
@Expose
private Tree tree;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProcessedLang() {
return processedLang;
}
public void setProcessedLang(String processedLang) {
this.processedLang = processedLang;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Tree getTree() {
return tree;
}
public void setTree(Tree tree) {
this.tree = tree;
}
String getName() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
누군가 내가 잘못 됐는지 알려 줄 수 있니? 너희들이 json 코드를보고 싶다면 나도 그걸 놓을 것임을 알리고 내가 놓지 않은 이유는 길기 때문이다. 어떻게 배열을 얻을 수 있을까? 어린이들에게 보여줄 것을 열거합니까? 시간 내 줘서 고마워.
는'getCategory'는 문자열을 반환하지만 당신은 당신의'for' 루프에서 반복하려는. 여기서 무엇을하려고합니까? – Zircon
죄송합니다. 언급하는 것을 잊어 버렸습니다. Arraylist Children을 전시하려고합니다. –