android中json转换成List<Map>
Java代码
package cn.anycall;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
/**
* 将json 数组转换为Map 对象
* @param jsonString
* @return
*/
public static Map<String, Object> getMap(String jsonString)
{
JSONObject jsonObject;
try
{
jsonObject = new JSONObject(jsonString); @SuppressWarnings("unchecked")
Iterator<String> keyIter = jsonObject.keys();
String key;
Object value;
Map<String, Object> valueMap = new HashMap<String, Object>();
while (keyIter.hasNext())
{
key = (String) keyIter.next();
value = jsonObject.get(key);
valueMap.put(key, value);
}
return valueMap;
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
/**
* 把json 转换为ArrayList 形式
* @return
*/
public static List<Map<String, Object>> getList(String jsonString)
{
List<Map<String, Object>> list = null;
try
{
JSONArray jsonArray = new JSONArray(jsonString);
JSONObject jsonObject;
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);
list.add(getMap(jsonObject.toString()));
}
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String temp = "[{\"aa\":\"1\",\"bb\":\"2\"},{\"aa\":\"3\",\"bb\":\"4\"},{\"aa\":\"5\",\"bb\":\"6\"}]";
List<Map<String, Object>> lm = Test.getList(temp);
for(int i=0;i<lm.size();i++){
System.out.println(lm.get(i).get("aa"));
System.out.println(lm.get(i).get("bb"));
}
}
}
相关新闻>>
- 发表评论
-
- 最新评论 更多>>