解决方案 »

  1.   

    把数据转一下就成了json格式的了。
    javaBean转成jsonpublic class JavaBeanToJson {
    public static void main(String[] args) {
    ArrayList<Student> list=new ArrayList<Student>();
    Student s1=new Student();
    s1.setName("leilei");
    s1.setAge(23);
    Student s2=new Student();
    s2.setName("leilei02");
    s2.setAge(23);
    list.add(s1);
    list.add(s2);

    StringWriter str=new StringWriter();

    ObjectMapper objectMapper=new ObjectMapper();
    try {
    objectMapper.writeValue(str, list);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(str);
    }
    }
    json返回javaBeanpublic class JsonToJavaBean {
    public static void main(String[] args) {
    String str="{\"student\":[{\"name\":\"leilei\",\"age\":23,\"gender\":true},{\"name\":\"leilei02\",\"age\":23,\"gender\":false}]}";
    Student stu = null;
    List<Student> list = null;
    try {
    ObjectMapper objectMapper=new ObjectMapper();
    StudentList studentList=objectMapper.readValue(str, StudentList.class);

    list=studentList.getStudent();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    for(Student s:list){
    System.out.println(s.getName()+"   "+s.getAge()+"      "+s.isGender());
    }
    }
    }
      

  2.   

    当然,还有一种Objectfrom的方法,也挺好用。
    例子就不发了
      

  3.   

    ObjectMapper类 是不是要下载jar?
      

  4.   

    java
    PrintWriter out = null;
    System.out.println("\n正在使用json格式将结果传回客户端:"+uMap.get(responseMan));
    String jsonText = "[{\"result\":\"添加工作计划成功!\",\"id\":\""+planItem.getFiwpitemId()+
    "\",\"planDate\":\""+planDate+"\"," +
    "\"content\":\""+content+"\",\"responseMan\":\""+uMap.get(responseMan)+"\"}]";
    // jsonText = "[{\"result\":\"添加工作计划成功!\"}]";
    try {
    out = this.getResponse().getWriter();
    JSONArray jsonArray = JSONArray.fromObject(jsonText); 
    // out.print("{'result':'添加工作计划成功!','id':'"+planItem.getFiwpitemId()+"','planDate':'"+
    // new SimpleDateFormat("yyyy-MM-dd").format(planItem.getFdplanDate())+"'," +
    // "'content':'"+content+"','responseMan':'"+user.getFcname()+"'}");
    out.print(jsonArray);// out.print(arr.toString());

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    out.flush();
    out.close();
    }
    客户端
    $.ajax({
    type:'post',//可选get
    url:'',//这里是接收数据的PHP程序
    data:{"planDate":datePlan,"content":content},//传给的数据,多个参数用&连接
    dataType:'json',//服务器返回的数据类型 可选XML ,Json jsonp script html text等
    success:function(result){
    result[0].planDate;
    result[0].id;
    result[0].content;
    result[0].responseMan
    //这里是ajax提交成功后,PHP程序返回的数据处理函数。msg是返回的数据,数据类型在dataType参数里定义!
    },
    error:function(){
    //ajax提交失败的处理函数!
    }
    });
      

  5.   

    谢谢楼上的回答,请问JSONArray在哪个包里?