You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中解析指定JSON输出并在JSP中展示所有RefNo

Java解析JSON并在JSP展示所有RefNo字段值

首先注意:你提供的原始内容不是标准JSON格式(外层是{Status=200,dataVal=...}这种Java对象toString的格式),需要先转换为标准JSON,再进行解析。

1. 转换为标准JSON

将外层的键值对格式修正为JSON语法,最终标准JSON如下:

{"Status":200,"dataVal":{"TotalCount":3,"Entities":[{"Type":50,"Message":"Submitted for Dispute","TransactionId":"2207280001","RefNo":"0110A2226A28","Amount":932.0,"AccountNo":"1049019264010","Vat":43.0},{"Type":50,"Message":"Submitted for Dispute","TransactionId":"2207280003","RefNo":"5815B2220B23","Amount":479.0,"AccountNo":"1049016101100","Vat":0.0},{"Type":50,"TransactionId":"2207280002","RefNo":"5815B2220B24","Amount":531.0,"AccountNo":"1049016101080","Vat":0.0}]}}

如果原始字符串是从外部获取的,可以用正则批量修正:

String rawStr = "{Status=200,dataVal={\"TotalCount\":3,...}}";
String standardJson = rawStr.replaceAll("(\\w+)=([^,}]+)", "\"$1\":$2");

2. Java端解析JSON(两种方式)

方式一:使用实体类绑定(Jackson库)

步骤1:引入Jackson依赖

如果用Maven,在pom.xml中添加:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

步骤2:创建对应实体类

// 顶层响应类
public class DisputeResponse {
    private int Status;
    private DataVal dataVal;

    // Getter和Setter
    public int getStatus() { return Status; }
    public void setStatus(int status) { Status = status; }
    public DataVal getDataVal() { return dataVal; }
    public void setDataVal(DataVal dataVal) { this.dataVal = dataVal; }
}

// DataVal类
public class DataVal {
    private int TotalCount;
    private List<Entity> Entities;

    // Getter和Setter
    public int getTotalCount() { return TotalCount; }
    public void setTotalCount(int totalCount) { TotalCount = totalCount; }
    public List<Entity> getEntities() { return Entities; }
    public void setEntities(List<Entity> entities) { Entities = entities; }
}

// Entity类(只保留需要的RefNo字段)
public class Entity {
    private String RefNo;

    public String getRefNo() { return RefNo; }
    public void setRefNo(String refNo) { RefNo = refNo; }
}

步骤3:Servlet中解析并传递数据到JSP

import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("/DisputeServlet")
public class DisputeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String standardJson = "{\"Status\":200,\"dataVal\":{\"TotalCount\":3,...}}"; // 替换为实际标准JSON字符串

        ObjectMapper mapper = new ObjectMapper();
        DisputeResponse responseObj = mapper.readValue(standardJson, DisputeResponse.class);

        // 提取所有RefNo
        List<String> refNoList = responseObj.getDataVal().getEntities().stream()
                .map(Entity::getRefNo)
                .collect(Collectors.toList());

        // 将数据存入request域
        request.setAttribute("refNoList", refNoList);
        // 转发到展示页面
        request.getRequestDispatcher("/showRefNo.jsp").forward(request, response);
    }
}

方式二:使用树模型解析(无需实体类)

如果不想创建实体类,可以直接用Jackson的树模型解析:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/DisputeServlet")
public class DisputeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String standardJson = "{\"Status\":200,\"dataVal\":{\"TotalCount\":3,...}}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(standardJson);
        JsonNode entitiesNode = rootNode.path("dataVal").path("Entities");

        List<String> refNoList = new ArrayList<>();
        for (JsonNode entityNode : entitiesNode) {
            refNoList.add(entityNode.path("RefNo").asText());
        }

        request.setAttribute("refNoList", refNoList);
        request.getRequestDispatcher("/showRefNo.jsp").forward(request, response);
    }
}

3. JSP页面展示RefNo

创建showRefNo.jsp,使用JSTL标签遍历展示:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>RefNo列表</title>
</head>
<body>
    <h2>所有RefNo字段值</h2>
    <ul>
        <c:forEach items="${refNoList}" var="refNo">
            <li>${refNo}</li>
        </c:forEach>
    </ul>
</body>
</html>

注意:需要引入JSTL依赖(Maven):

<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

内容的提问来源于stack exchange,提问作者Md. Sajjad Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:06:16