gpt4 book ai didi

java - 无法获取 json 的所有值

转载 作者:行者123 更新时间:2023-11-30 08:06:12 24 4
gpt4 key购买 nike

我正在尝试使用 rest webservice 将从数据库获取的值返回到前端 View ,但问题是它只返回 json 值的一行(第一行)

我有这样的 json:

{"jobName":"NASA Scientist","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"Sample 2","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"NASA Scientist","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Web developer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"XYZ 123","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Web developer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Programmer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Programmer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}

它只将第一行 json 值返回给前端,而不是全部。我将从数据库中获取的值添加到 List 中,并像这样在 webservice 中公开它:

public class FetchJobSummaryDAO {

public List getJobSummaries() {

JobSummaries jobSummaries = new JobSummaries();
List<JobSummaries> jobSummaryList = new ArrayList<JobSummaries>();

try {
Connection con = DBConnection.getConnection();

String query = "select JOB_NAME,JOB_DESCRIPTION,JOB_ROLE,JOB_PRIMARY_SKILL,JOB_DESIGNATION,JOB_EXP,JOB_SAL_RANGE, JOB_POSTEDBY from TBL_JOBPOSTING";

PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();

ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);

for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) {
System.out.print(", ");
}
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " " + columnValue);
}
System.out.println("");
}

} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;
}

网络服务:

@Path("/FetchJobSummary")
public class FetchJobSummaryService {
FetchJobSummaryDAO dao = new FetchJobSummaryDAO();

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<JobSummaries> fetch() {
List js = dao.getJobSummaries();
for(int i=0; i< js.size(); i++){
System.out.println(js); //prints values with same hashcode
}
return dao.getJobSummaries();
}
}

当我打印列表时,它会打印具有相同哈希码的值,所以我猜它只获得一个 json 值

[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]

最佳答案

这样改

public class FetchJobSummaryDAO {

public List getJobSummaries() {

JobSummaries jobSummaries = null;
List<JobSummaries> jobSummaryList = new ArrayList<JobSummaries>();

try {

Connection con = DBConnection.getConnection();

String query = "select JOB_NAME,JOB_DESCRIPTION,JOB_ROLE,JOB_PRIMARY_SKILL,JOB_DESIGNATION,JOB_EXP,JOB_SAL_RANGE, JOB_POSTEDBY from TBL_JOBPOSTING";

PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();

ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
jobSummaries = new JobSummaries()
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);

for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) {
System.out.print(", ");
}
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " " + columnValue);
}
System.out.println("");
}

} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;

关于java - 无法获取 json 的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com