JsonArray遍歷方法
阿新 • • 發佈:2019-02-05
步驟
①、轉為JsonArray格式
②、使用Iterator進行遍歷準備
String relation = "[{"codeId":"1","codeName":"你們"},{"codeId":"2","codeName":"他們"}]";
JSONArray ja=JSON.parseArray(relation);
Iterator<Object> it = ja.iterator();
List<PubCodeModel> list=new ArrayList<PubCodeModel>();
while (it.hasNext()) {
JSONObject ob = (JSONObject) it.next();
PubCodeModel model = null;
if(ob.getString("codeId")!=null){
model=new PubCodeModel();
model.setVcode(ob.getString("codeId"));
}
if (ob.getString("codeName")!=null){
model.setVname(ob.getString("codeName"));
}
if(model!=null){
list.add(model);
}
}
關鍵CODE
Iterator<Object> it = ja.iterator();
while (it.hasNext()) {
JSONObject ob = (JSONObject) it .next();
}