public static void main(String[] args)
{
String string = "[{\"depid\":\"5\",\"score\":\"10\"},{\"depid\":\"4\",\"score\":\"40\"},{\"depid\":\"4\",\"score\":\"30\"},{\"depid\":\"5\",\"score\":\"30\"}]";
JSONArray fromObject = JSONArrayfromObject(string);
Map<String,Integer> map = new HashMap<String, Integer>();
for (Object object : fromObject)
{
JSONObject jsonObject = (JSONObject) object;
String depid = (String)jsonObjectget("depid");
Integer score = IntegervalueOf((String)jsonObjectget("score"));
if (mapcontainsKey(depid))
{
int integer = mapget(depid);
mapput(depid, integer+score);
}
else
{
mapput(depid, score);
}
}
Set<Entry<String, Integer>> entrySet = mapentrySet();
JSONArray jsonArray = new JSONArray();
for (Entry<String, Integer> entry : entrySet)
{
JSONObject jsonObject = new JSONObject();
jsonObjectput("depid",entrygetKey());
jsonObjectput("score",StringvalueOf(entrygetValue()));
jsonArrayadd(jsonObject);
}
Systemoutprintln(jsonArraytoString());
}
var a=[{"list":[{"count":0,"createTime":"2008-01-25 00:00:00","createUserDept":"","createUserDeptId":"","createUserId":"","createUserName":"","deleteFlag":"0","introduction":"","month":"","pagerSize":0,"periodicalId":"","periodicalName":"admin","periodicalType":"","publishDate":null,"status":"0","updateUserDept":"","updateUserDeptId":"","updateUserId":"","updateUserName":"","year":""},{"count":0,"createTime":"2008-01-25 00:00:00","createUserDept":"","createUserDeptId":"","createUserId":"","createUserName":"","deleteFlag":"0","introduction":"","month":"","pagerSize":0,"periodicalId":"","periodicalName":"admin","periodicalType":"","publishDate":null,"status":"0","updateUserDept":"","updateUserDeptId":"","updateUserId":"","updateUserName":"","year":""}],"pageString":""}]
var list=a[0]list;
for(i=0;i<listlength;i++){
alert(list[i]count);
alert(list[i]createTime);
}
建立对应实体类,将json字符串序列化成对象
//注:主类名默认Root,可修改public class UserInfoList
{
public int subscribe { get; set; }
public string openid { get; set; }
public string nickname { get; set; }
public int sex { get; set; }
public string language { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string headimgurl { get; set; }
public long subscribe_time { get; set; }
public string unionid { get; set; }
public string remark { get; set; }
public int groupid { get; set; }
}
public class Root
{
public IList<UserInfoList> user_info_list { get; set; }
}
这是c#的
haojson点com/json2csharp/ 这个网址 可以在线json字符串生成实体类
以上就是关于从API接口获取的json数据怎么存到对象的List集合中全部的内容,包括:从API接口获取的json数据怎么存到对象的List集合中、json对象的值为数组,怎么获取数组某项的值(注意不是json数组)、java中从josn中取出相同key所对应的另一个key的集合等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)