Java解析JSON

jsp文件

var people = { "programmers": [{ "firstName": "Elliotte", "lastName":"Harold","email": "[email protected]" }],
"authors": [{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }],
  "musicians": [{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
  { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }]
  }; 
  // var obj = eval('(' + str + ')');   


    createXMLHttpRequest()
var url = "parseJson.do?people=" + people.toJSONString();   

XMLHttpReq.open("get",url,true);
// XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLHttpReq.onreadystatechange=processResponse;
XMLHttpReq.send(null);


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 {
// TODO 自动生成方法存根

String jsonString = request.getParameter("people");   

JSONObject jb = JSONObject.fromObject(jsonString);   
JSONArray array=jsonObj.getJSONArray("programmers");
System.out.println("array:"+array.get(0));
JSONObject obj=jsonObj.getJSONObject("singer");
System.out.println("obj:"+obj.get("firstName"));
return null;
}


}

你可能感兴趣的:(java,apache,json,struts,servlet)