js、java处理json数据方法
一:js处理json数据
处理办法之一是把本机json数据或远程返回json数据用eval函数,使之变成DOM对象。
例如:
var people = {
"programmers":[{"firstName":"Elie" , "lastName":"Harld" , "email": "elro@maq.com" }],
"authors": [{"firstName":"Isac" , "lastName":"Asiov" , "genre": "science fiction" }],
"musicians": [{"firstName":"Eric" , "lastName":"Claon" , "instrument": "guitar" },
{"firstName":"Seei" , "lastName":"Rnoff" , "instrument": "piano" }]
};
二:Java处理json数据
var url = "parseJson.do?people=" + escape(people.toJSONString());
java 文件
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
public class JsonAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String param=request.getParameter("people");
JSONObject jsonObj=JSONObject.fromObject(param);
for (Iterator iter = jsonObj.keys(); iter.hasNext();) { //先遍历整个 people 对象
String key = (String)iter.next();
System.out.println(key);
JSONArray array = jsonObj.getJSONArray(key);
for (int i = 0; i < array.size(); i++) { //再遍历整个三个属性(programmers,authors,musicians)的值(数组)
JSONObject object = (JSONObject) array.get(i);
for (Iterator iterator = object.keys(); iterator.hasNext();) { //最后遍历值中的每个属性
String key2 = (String) iterator.next();
System.out.print(" "+key2+" : "+object.get(key2));
}
System.out.println();
}
System.out.println();
}
//最终输出如果如下:
programmers
firstName : Elie lastName : Harld email : elro@maq.com
authors
firstName : Isac lastName : Asiov genre : science fiction
musicians
firstName : Eric lastName : Claon instrument : guitar
firstName : Seei lastName : Rnoff instrument : piano
}
一:js处理json数据
处理办法之一是把本机json数据或远程返回json数据用eval函数,使之变成DOM对象。
例如:
var people = {
"programmers":[{"firstName":"Elie" , "lastName":"Harld" , "email": "elro@maq.com" }],
"authors": [{"firstName":"Isac" , "lastName":"Asiov" , "genre": "science fiction" }],
"musicians": [{"firstName":"Eric" , "lastName":"Claon" , "instrument": "guitar" },
{"firstName":"Seei" , "lastName":"Rnoff" , "instrument": "piano" }]
};
二:Java处理json数据
var url = "parseJson.do?people=" + escape(people.toJSONString());
java 文件
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
public class JsonAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String param=request.getParameter("people");
JSONObject jsonObj=JSONObject.fromObject(param);
for (Iterator iter = jsonObj.keys(); iter.hasNext();) { //先遍历整个 people 对象
String key = (String)iter.next();
System.out.println(key);
JSONArray array = jsonObj.getJSONArray(key);
for (int i = 0; i < array.size(); i++) { //再遍历整个三个属性(programmers,authors,musicians)的值(数组)
JSONObject object = (JSONObject) array.get(i);
for (Iterator iterator = object.keys(); iterator.hasNext();) { //最后遍历值中的每个属性
String key2 = (String) iterator.next();
System.out.print(" "+key2+" : "+object.get(key2));
}
System.out.println();
}
System.out.println();
}
//最终输出如果如下:
programmers
firstName : Elie lastName : Harld email : elro@maq.com
authors
firstName : Isac lastName : Asiov genre : science fiction
musicians
firstName : Eric lastName : Claon instrument : guitar
firstName : Seei lastName : Rnoff instrument : piano
}