jsp 传 JSON 到 JAVA 后台处理的问题 本帖最后由 lkaidy 于 2014-04-22 16:15:51 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只能写成json2,后台处理:String jsonStr = "{'user':[{'uname':'abcd','password':'afdfdfd'},{'uname':'afdfsdf','password':'afsdfsdf'}]}"; JSONObject json = JSONObject.fromObject(jsonStr); JSONArray ja = (JSONArray) json.get("user"); for (Object object : ja) { JSONObject jo = (JSONObject) object; System.out.println(jo.get("uname") + "\t" + jo.get("password")); } 我的json 没有 JSONObject.fromObject() 这个方法。如果用Gson 也能处理这个问题吗? fromObject()是GSON里面的方法啊 两种写法都可以 但是解析方式不一样啊 第一中解析成对象数组 第二种解析成对象(这个对象里面包含属性:user对象,user 对象的类型是对象数组。 这样你必须有这样一个实体,根据这个实体来造型或是解析)。 只能是json1, 因为json1是字符串,json2 要是定义成字符串,也能传到后台。是json对象就不成了 。 import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class JsonUtils {public static String encodeList(Object[] objs) {return encodeList(Arrays.asList(objs));}@SuppressWarnings("unchecked")public static String encodeList(Collection objs) {JSONArray list = new JSONArray();if (objs == null || objs.size() == 0)return list.toString();for (Object ae : objs) {list.add(ae);}return list.toString();}@SuppressWarnings("unchecked")public static String encodeObject(Object obj) {if (obj instanceof Collection)return encodeList((Collection) obj);JSONObject jo = JSONObject.fromObject(obj);return jo.toString();}@SuppressWarnings("unchecked")public static <T> List<T> decodeList(String str, Class<T> clazz) {if (str == null || "".equals(str))return null;JSONArray obj = JSONArray.fromObject(str);Object[] rts = obj.toArray();List<T> result = new ArrayList<T>(rts.length);for (int i = 0; i < rts.length; i++) {Object jo = rts[i];T ele = (T) JSONObject.toBean((JSONObject) jo, clazz);result.add(ele);}return result;}@SuppressWarnings("unchecked")public static <T> T decodeObject(String json, Class<T> clz) {JSONObject jsonObject = JSONObject.fromObject(json);T bean = (T) JSONObject.toBean(jsonObject, clz);return bean;}public static void main(String[] args) {DTO d = new DTO();d.setName("dto");Son realSone = new Son();realSone.setName("father,son");realSone.setSonName("son1");realSone.setAge(1);d.setRealSon(realSone);Son realSone2 = new Son();realSone.setName("father,son2");realSone.setSonName("son2");realSone.setAge(2);d.setSon(realSone2);List<Father> sonList = new ArrayList<Father>();sonList.add(realSone);sonList.add(realSone2);d.setSonList(sonList);String json=encodeObject(d);System.out.println(encodeObject(d));//JSONObject parseObject = JSON.parseObject(json);JSONObject fromObject = JSONObject.fromObject(json);Map<String, Class> classMap = new HashMap<String, Class>();classMap.put("realSon", Son.class);classMap.put("son", Son.class);classMap.put("sonList", Father.class);DTO newDto = (DTO) fromObject.toBean(fromObject, DTO.class, classMap);System.out.println(newDto.getSonList().get(0).getName());}} 传输的问题我自已解决了,要把AJAX的data部分写成 data:"testJson="+JSON.stringify(Json2), 这样写就能传输成功,现在有个问题,这种格式的JSON传到后台要怎么处理呢。 {"Rows":[ {"Cells":[ {"CellValue":"100","Alligment":0}, {"CellValue":"90.8","Alligment":0}, ]}, {"Cells":[ {"CellValue":"100","Alligment":0}, {"CellValue":"90.8","Alligment":0}, ]}, {"Cells":[ {"CellValue":"100","Alligment":0}, {"CellValue":"90.8","Alligment":0} ]}]} jquery 1.7 实现时间显示兼容IE6 iframe的onload事件在firefox浏览器下不起作用 日期格式化 xtree问题 javascript如何 读取和写入xml文件 求一个 正则表达式 JavaScript极限难题 javascript 拖动排序代码 请各位大侠帮我解释段代码。 在SCRIPT中如何调用按钮中的ONCLICK事件 急!!急!!FusionCharts自动更新实现实时数据监测问题 【紧急求助】问一个JS关于手机移动端触屏滚动的
String jsonStr = "{'user':[{'uname':'abcd','password':'afdfdfd'},{'uname':'afdfsdf','password':'afsdfsdf'}]}";
JSONObject json = JSONObject.fromObject(jsonStr);
JSONArray ja = (JSONArray) json.get("user");
for (Object object : ja) {
JSONObject jo = (JSONObject) object;
System.out.println(jo.get("uname") + "\t" + jo.get("password"));
}
我的json 没有 JSONObject.fromObject() 这个方法。如果用Gson 也能处理这个问题吗?
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtils {public static String encodeList(Object[] objs) {
return encodeList(Arrays.asList(objs));
}@SuppressWarnings("unchecked")
public static String encodeList(Collection objs) {
JSONArray list = new JSONArray();
if (objs == null || objs.size() == 0)
return list.toString();
for (Object ae : objs) {
list.add(ae);
}
return list.toString();
}@SuppressWarnings("unchecked")
public static String encodeObject(Object obj) {
if (obj instanceof Collection)
return encodeList((Collection) obj);
JSONObject jo = JSONObject.fromObject(obj);
return jo.toString();
}@SuppressWarnings("unchecked")
public static <T> List<T> decodeList(String str, Class<T> clazz) {
if (str == null || "".equals(str))
return null;
JSONArray obj = JSONArray.fromObject(str);
Object[] rts = obj.toArray();
List<T> result = new ArrayList<T>(rts.length);
for (int i = 0; i < rts.length; i++) {
Object jo = rts[i];
T ele = (T) JSONObject.toBean((JSONObject) jo, clazz);
result.add(ele);
}
return result;
}@SuppressWarnings("unchecked")
public static <T> T decodeObject(String json, Class<T> clz) {
JSONObject jsonObject = JSONObject.fromObject(json);
T bean = (T) JSONObject.toBean(jsonObject, clz);
return bean;
}public static void main(String[] args) {
DTO d = new DTO();
d.setName("dto");Son realSone = new Son();
realSone.setName("father,son");
realSone.setSonName("son1");
realSone.setAge(1);
d.setRealSon(realSone);Son realSone2 = new Son();
realSone.setName("father,son2");
realSone.setSonName("son2");
realSone.setAge(2);
d.setSon(realSone2);List<Father> sonList = new ArrayList<Father>();
sonList.add(realSone);
sonList.add(realSone2);
d.setSonList(sonList);
String json=encodeObject(d);
System.out.println(encodeObject(d));//JSONObject parseObject = JSON.parseObject(json);JSONObject fromObject = JSONObject.fromObject(json);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("realSon", Son.class);
classMap.put("son", Son.class);
classMap.put("sonList", Father.class);
DTO newDto = (DTO) fromObject.toBean(fromObject, DTO.class, classMap);
System.out.println(newDto.getSonList().get(0).getName());
}
}
data:"testJson="+JSON.stringify(Json2),
这样写就能传输成功,现在有个问题,这种格式的JSON传到后台要怎么处理呢。 {"Rows":[
{"Cells":[
{"CellValue":"100","Alligment":0},
{"CellValue":"90.8","Alligment":0},
]},
{"Cells":[
{"CellValue":"100","Alligment":0},
{"CellValue":"90.8","Alligment":0},
]},
{"Cells":[
{"CellValue":"100","Alligment":0},
{"CellValue":"90.8","Alligment":0}
]}
]}