做过的一些json数据解析 几种常用的json数据格式解析public static Person getPerson(String key,String jsonString){
Person person = new Person();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject personObject = jsonObject.getJSONObject("person");
person.setId(personObject.getInt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
} catch (Exception e) {
// TODO: handle exception
}
return person;
}
public static List getPersons(String key,String jsonString){
List list = new ArrayList();
try {
JSONObject jsonObject = new JSONObject(jsonString);
//返回json数组
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Person person = new Person();
person.setId(jsonObject2.getInt("id"));
person.setName(jsonObject2.getString("name"));
person.setAddress(jsonObject2.getString("address"));
list.add(person);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List getList(String key,String jsonString){
List list = new ArrayList();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i
String msg = jsonArray.getString(i);
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List> getListMap(String key,String jsonString){
List> list = new ArrayList>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map map = new HashMap();
Iterator iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String json_key = iterator.next();
Object json_value = jsonObject2.get(json_key);
if (json_value == null) {
json_value = "";
}
map.put(json_key, json_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}