android中将json转为list
String=[{"count":1,"gid":100000,"id":1,"text":"100000"},{"count":70484,"gid":100000,"...
String =
[
{"count":1,"gid":100000,"id":1,"text":"100000"},
{"count":70484,"gid":100000,"id":1,"text":"101252"}
]
目标: 把id的值放入list_id中 -----list_id 为List
把text的值放入list_text中-list_text 为List
求大神指点 展开
[
{"count":1,"gid":100000,"id":1,"text":"100000"},
{"count":70484,"gid":100000,"id":1,"text":"101252"}
]
目标: 把id的值放入list_id中 -----list_id 为List
把text的值放入list_text中-list_text 为List
求大神指点 展开
展开全部
String json = "{'count':1,'gid':100000,'id':1,'text':'100000'},{'count':70484,'gid':100000,'id':1,'text':'101252'}";
//把json字符串转化为json对象
JSONObject jsonObject = JSONObject.fromObject(json);
ArrayList<obj> list = new ArrayList<obj>();
//遍历json对象
for(int i=0;i<jsonObject .size();i++){
String s = jsonObject .getString(i);
JSONObject data = JSONObject.fromObject(s);
obj obj1 = new obj();
obj1.list_id=data.getString("id");
obj1.list_text = data.getString("text");
list.add(obj1);
}
class obj{
private string list_id ;
private string list_text ;
}
更多追问追答
追问
String s = jsonObject.getString(i);
这行不明白,报错:参数不适合
list_id 是个list集合,list_text是另一个集合,你这个是把他们俩当参数了。如果
是放入2个不同的集合,是不是也是这个思路
追答
不好意思,json 字符串被我写错了。
String =
[
{"count":1,"gid":100000,"id":1,"text":"100000"},
{"count":70484,"gid":100000,"id":1,"text":"101252"}
]
是的,思路一样
2015-10-22 · 知道合伙人软件行家
关注
展开全部
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"));
}
}
}
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"));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询