传过来的参数:jsonData=[{‘id’:‘1’,‘name’:‘xiaowang’},…]
第一:使用JSONObject解析
try{
JSONArray jsonArray = new JSONArray(jsonData);
for(int i=0;i<jsonArray.length;i++){
JSONObject jsonObject = jsonArray.getJSONObject(i); String id =
jsonObject.getString("id"); String name =
jsonObject.getString("name");
}
}catch(Exception e){
...
}
第二:使用GSON
//App是一个实体类
Gson gson = new Gson();
List<App> applist = gson.fromJson(jsonData,new TypeToken<<List<App>>>().getType());
//如果只有一组使用:
Gson gson = new Gson();
App app = gson.fromJson(jsonData,App.class);
第三:使用JackSon
try{
JSONMapper jsonMapper = new JSONMapper();
App app = jsonMapper.readValue(jsonData,App.class);
}catch(Exception e){
...
}
第四:使用FastJson
JSONArray jsonArray = JSONArray.parseArray(jsonData);
for(Iterator i =jsonArray.iterator; i.hasNext() ){
JSONObject object = (JSONObject)i.next():
String id = object.get("id").toString();
String name = objrct.get("name").toString();
}
App:
public Class App{
private String id ;
private String name;
public void setId(String id){
this.id = id
}
public String getId(){
return this.id
}
public void setName(String name){
this.name= name
}
public String getName(){
return this.name
}
}