struts2 初学系列(二) 返回json数据

接着(一)继续写一个返回json格式的数据的demo。 返回json有注解的方法,有xml的方法,学一下xml的方法。 首先需要在上一个工程基础上添加一个jar包:struts2-json-plugin-2.3.20.jar 和上一个例子基本一样,需要修改struts.xml和HelloWorld.java的程序。 先看HelloWorld.java:

public class HelloWorld {
private Student[] students;

/**
 * @return the students
 */
public Student[] getStudents() {
    return students;
}


/**
 * @param students the students to set
 */
public void setStudents(Student[] students) {
    this.students = students;
}

public String showMessage(){

    student = new Student();
    student.setId("123");
    student.setName("刘德华");
    Student s = new Student();
    s.setId("321");
    s.setName("周杰伦");
    students = new Student[2];
    students[0] = student;
    students[1] = s;

    return "hello";
}
}

里面定义了一个private变量students,需要将这个变量变为json格式返回。 首先需要定义一个Student的类,里面有name和id两个属性和get set方法即可。 然后提供students的set get方法,然后在showMessage()方法里面给students实例化赋值。 然后这个方法就可以了,接下来就是修改struts.xml的配置:

    <struts>  
       <package name="tk.lvlvforever" extends="json-default">  
          <action name="test_*" class="tk.lvlvforever.HelloWorld" method="{1}">    
               <result type="json" name="hello">
                <param name="root">students</param>
               </result> 

               <result type="json" name="hell">
                <param name="root">student</param>

               </result>  
          </action>  
       </package>  

    </struts>  

其中主要就是extends改为json-default,下面的格式注意下。

            <result type="json" name="hello">
                <param name="root">students</param>
               </result> 

name="hello"是对应方法里面的返回的字符串, 带着name="root"的结果:

[{"id":"123","name":"刘德华"},{"id":"321","name":"周杰伦"}]       

name=""是:

{"student":{"id":"123","name":"刘德华"},"students":[{"id":"123","name":"刘德华"},{"id":"321","name":"周杰伦"}]}

解析json字符串

现在看下如何解析字符串。需要的包为:

  • json-lib-2.3-jdk15.jar
  • commons-beanutils-1.8.0.jar
  • commons-collections-3.1.jar
  • commons-lang-2.4.jar
  • ezmorph-1.0.6.jar
  • commons-logging-1.1.3.jar

比如上面的例子: 首先用java 的net网络连接方法获取json字符串,然后进行解析。看代码:

public static void main(String[] args) throws IOException{
    URL url = new URL("http://localhost:8192/struts2/test_showMessage"); 
    Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
    StringBuffer sb = new StringBuffer();
    BufferedReader bufferedReader = new BufferedReader(reader);
    int c;
    while((c=bufferedReader.read()) != -1){
        sb.append((char)c);
    }
    bufferedReader.close();
    reader.close();
    String jsonString = sb.toString();
    System.err.println(jsonString);
    JSONArray jsonArray = JSONArray.fromObject(jsonString);

    Student[] students = new Student[jsonArray.size()];
    for(int i=0; i < jsonArray.size();i++){
        students[i] = (Student)JSONObject.toBean(jsonArray.getJSONObject(i),Student.class); 
    }
    System.err.println(students[0].getId()+students[0].getName()+"\n"+students[1].getId()+students[1].getName());

}

这样就可以把json字符串在转回POJO对象了。 感觉这么做把对象变成字符串然后再变回来适合简单点的对象,太复杂的对象比较麻烦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值