JSONの構文解析の理解

  

JSONObject用于描述JSON对象JSONArray用于描述JSON数组
文字列str = "{\"名\ "アレン\" \ \ "年齢\":20} ";
JSONObject OBJ =新しいJSONObject(STR);
文字列名= obj.getString( "名前");
int型の年齢= obj.getInt( "年齢");

JSONArray
文字列str = "[10、真、\"こんにちは\ "]";
JSONArray OBJ =新しいJSONArray(STR);
文字列名= obj.getString(2);
int型の年齢= obj.getInt(0)。

 

 

服务端ListEmpServlet重构代码如下:
プライベート文字列parseJSON(一覧<Empを> EMPS){
StringBufferのSB =新規のStringBuffer();
sb.append( "[")。
以下のために{(INT i = 0; iは++; iがemps.size()<)
のemp E = emps.get(i)を、
sb.append( "{\"名\ ":\" "+ e.getName()+" \ "\ "年齢\": "+ e.getAge()+"、\ "給与\":" + e.getSalary()+」\ "性別\" ''。 "+ e.getGender()+" \ "+ \"のID \ ";" + e.getId()+ "}");
}
SB .deletedCharAt(sb.length() - 1);
sb.append( "]");
(sb.toStringを返します);
}
}

ます。https://my.oschina.net/yourpapa/blog/621600で再現

おすすめ

転載: blog.csdn.net/weixin_34364135/article/details/92033260