-
达令说
/**
*
* @param result JSON字符串
* @param name JSON数组名称
* @param fields JSON字符串所包含的字段
* @return 返回List<Map<String,Object>>类型的列表,Map<String,Object>对应于 "id":"1"的结构
*/
public static List<Map<String, Object>> convertJSON2List(String result,
String name, String[] fields) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
JSONArray array = new JSONObject(result).getJSONArray(name);
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.opt(i);
Map<String, Object> map = new HashMap<String, Object>();
for (String str : fields) {
map.put(str, object.get(str));
}
list.add(map);
}
} catch (JSONException e) {
Log.e("error", e.getMessage());
}
return list;
}
-
白板的微信
做过的一些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<Person> getPersons(String key,String jsonString){
List<Person> list = new ArrayList<Person>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
//返回json数组
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); 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<String> getList(String key,String jsonString){
List<String> list = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
String msg = jsonArray.getString(i);
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List<Map<String, Object>> getListMap(String key,String jsonString){
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> 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;
}
-
qq_花开花谢_0
如果有用Struts框架那就好办了,struts.xml里配置继承json-defual,一般Struts包都有,在action下个方法,一样的返回结果 struts.xml中<result name="返回字符串" type="json">
<param name="includeProperties">传出去的值,传出去的值,...</param>
</result>页面异步刷新的写法