如何使用JSTL从JSON字符串输出值? [英] How do I use JSTL to output a value from a JSON string?

查看:1039
本文介绍了如何使用JSTL从JSON字符串输出值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JBoss 7.1.3.As.Final,并正在构建Spring 3.2.11.RELEASE Web应用程序.在我的JSP页面上,有这个

I’m using JBoss 7.1.3.As.Final and am building a Spring 3.2.11.RELEASE web application. On my JSP page I have this

${jsonString}

,我想知道的是,假设此json字符串具有属性名称",我如何使用JSTL(最好没有scriptlet)将与名称"属性关联的值打印到页面上?毫不奇怪,这是行不通的

and what I would like to know is assuming this json string has an attribute "name", how do I use JSTL (preferably no scriptlets) to print out the value associated with the "name" attribute to my page? Unsurprisingly, this doesn’t work

${jsonString[‘name’]}

推荐答案

如果您可以使用第三方库(例如Jackson),则完成此功能应该相当简单.不过,您仍然必须创建一些Java文件才能使其正常工作.首先,创建一个与您的json数据匹配的POJO(在您的情况下,Employee可能是其他东西,但您的POJO应该与您的字段匹配).

If you can use third party libraries (e.g. Jackson), it should be fairly simple to accomplish this functionality. You will still have to create some java files to make this work though. First, create a POJO that matches your json data (in your case Employee could be something else but your POJO should match your fields).

public class Employee{
private String name;
private int age;
private String company;
public String getName(){
    return name;
}
public String getCompany(){
    return company;
}
public Integer getAge(){
    return age;
}
//implement setters
}

接下来,像这样为Employee类创建一个列表包装器

Next, create a list wrapper for Employee class like this

 public class EmployeeList {
 public List<Employee> employees=new ArrayList<Employee>(); 
 }

现在,创建一个JsonParser类(将Jackson库以及应用程序li​​b文件夹添加到应用程序类路径中)

Now, create a JsonParser class (add Jackson library to the app classpath as well your app lib folder)

 import org.codehaus.jackson.map.ObjectMapper;

 public class JsonParser {  
 ObjectMapper objectMapper=new ObjectMapper();
 public <T> T parseJson(String json,Class<T> targetType)throws Exception{   
//uncomment this line if you want to ignore some fields, they dont have to   be in your POJO then    
 //objectMapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

 return objectMapper.readValue(json, targetType);
}
    }

请注意,JsonParser可以处理任何类型,而不仅仅是Employee.现在,在您的jsp中 添加以下导入(将jstl-1.2.jar添加到您的类路径以及您的应用程序li​​b文件夹中)

Note that JsonParser can handle any type not just Employee. Now, in your jsp add the following import (add jstl-1.2.jar to your classpath as well as your app lib folder)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

将以下代码添加到正文部分

Add the following code to the body section

<body>
<%@ page import="java.util.List"%>
<%@ page import="apps.simpleweb.json.JsonParser"%>
<%@ page import="apps.simpleweb.data.Employee"%>
<%@ page import="apps.simpleweb.data.EmployeeList"%>
<%
//example json string ; note that employees matches the list name
String jsonString = "{  \"employees\":  [ {\"name\": \"Peter\", \"age\":  25, \"company\": \"XXXX\" },{ \"name\": \"Mark\", \"age\":45, \"company\": \"XXXX\"} ] }";

JsonParser parser = new JsonParser();
EmployeeList empList = parser.parseJson(jsonString, EmployeeList.class);
request.setAttribute("employeeList", empList.employees);
%>
 <c:forEach items="${employeeList}" var="employee" >
<c:out  value="Name : ${employee.name}" />
<c:out  value="Age : ${employee.age}"   />
 </c:forEach>

如果将解析移到servlet,应该能够完全避免scriptlet代码.

You should be able to avoid scriptlet code entirely if you move the parsing to the servlet.

这篇关于如何使用JSTL从JSON字符串输出值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆