使用List<Object>封装的JsonObject等类的读取方法
2017-01-22 17:23
519 查看
例如:
JSONObject first = new JSONObject();
first.put("first", 1);
first.put("first2", 2);
first.put("first3", 3);
JSONObject second = new JSONObject();
second.put("second", first);
JSONObject third = new JSONObject();
third.put("third", second); ////现在JSONObject类对象third是一个由三层JSONObject构成的
则其读取方法可采用如下:
public List<Object> readJSONObject( JSONObject json ){
List<Object> result = new ArrayList<Object>();
int type = 5;
JSONObject origin = json.getJSONObject( "third" ).getJSONObject("second");
////此时origin和first是相同的
//若json是List<List<Object>>形式,则采用JSONObject rst= (Double) json.get(i).get(1)进行读取
List<Object> list = getObject(origin, type);
result.addAll(list);
}
public List<Object> getObject(JSONObject json, int type ){
List<Object> result = new ArrayList<Object>();
for(Object k :
json.keySet()) {
////依次读取json中数据
String key = (String) k; //
String[] spaces = query.split(","); //对于用某符号分割的字符串采用此法
Integer add1 = origin.optInt(key, 0)+1;
// for(String space : spaces) {
}
JSONObject rst = new JSONObject();
rst.put(key, add1);
result.add(rst);
}
return result;
}
JSONObject first = new JSONObject();
first.put("first", 1);
first.put("first2", 2);
first.put("first3", 3);
JSONObject second = new JSONObject();
second.put("second", first);
JSONObject third = new JSONObject();
third.put("third", second); ////现在JSONObject类对象third是一个由三层JSONObject构成的
则其读取方法可采用如下:
public List<Object> readJSONObject( JSONObject json ){
List<Object> result = new ArrayList<Object>();
int type = 5;
JSONObject origin = json.getJSONObject( "third" ).getJSONObject("second");
////此时origin和first是相同的
//若json是List<List<Object>>形式,则采用JSONObject rst= (Double) json.get(i).get(1)进行读取
List<Object> list = getObject(origin, type);
result.addAll(list);
}
public List<Object> getObject(JSONObject json, int type ){
List<Object> result = new ArrayList<Object>();
for(Object k :
json.keySet()) {
////依次读取json中数据
String key = (String) k; //
String[] spaces = query.split(","); //对于用某符号分割的字符串采用此法
Integer add1 = origin.optInt(key, 0)+1;
// for(String space : spaces) {
}
JSONObject rst = new JSONObject();
rst.put(key, add1);
result.add(rst);
}
return result;
}
相关文章推荐
- JS 使用List<Object> 对象中的Object对象的某个属性的临时处理方法
- java 后台使用gson 将json字符串转换为List<Map<String,object>> 数组
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- List<Object>及 JSONArray互转工具方法
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- 使用 fastjson将字符串转为 list<map<string,object>>
- 从数据库读取数据存放在List<T>中,在jsp中将其取出来。(使用JSON)
- Android学习笔记3 使用Intent传递复杂的数据(对象,List<Object>等)
- android .向sdcard写入json字符串、读取json字符保存到list<类>中供调用
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值 .
- java如何解析json字符串(List<Map<String,Object>成功的类型)
- 将List<Object[]>按照某字段值分类,对数据重新封装。
- VS2010使用扩展方法对List<T>进行随机排序
- .Net:再现List<T>:相关方法使用
- ASP.NET List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- FreeMarker解析List<JSONObject>数据的疑惑
- 【转载】[C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- 读取xml文件转成List<T>对象的两种方法(附源码)