如何有键在我的Json,而不仅仅是值



我从primevue创建了一个数据表,但是我只得到空行。

我看了看它可能来自哪里,我认为它来自我的Json,其字段没有键。应该做哪些修改,使Json再次成为标准,我的数据可以显示?提前谢谢。

我得到的Json的一个简短示例:

[
[
32,
"DE BELLOUET Jag",
"1.3.13.3.",
"Cid polseruti sau"
],
[
15,
"NOURAUD Benjamin",
"1.3.13.3.",
"Cid polseruti sau"
]
]

我需要的jSon的简短示例:

[
[
"id":32,
"fullName":"DE BELLOUET Jag",
"acs":"1.3.13.3.",
"nom_service":"Cid polseruti sau"
],
[
"id":15,
"fullName":"NOURAUD Benjamin",
"acs":"1.3.13.3.",
"nom_service":"Cid polseruti sau"
]
]

主程序:gererpersones .vue

<template>
<div class="principal_personnes">
<div class="bandeau">
<div>{{titre}}</div>
</div>
<div>
<h2>Liste des agents actifs</h2>
<DataTable :value="agents" :paginator="true" :rows="10" class="tableau">
<template #empty>
Pas d'agent trouvé.
</template>
<Column field="id" :sortable="true" header="Id" headerStyle="width: 3em" bodyStyle="text-align: center"></Column>
<Column field="fullName" :sortable="true" header="Nom - Prénom" headerStyle="width: 250px"></Column>
<Column field="service.acs" :sortable="true" header="A.C.S." headerStyle="width: 150px"></Column>
<Column field="service.nom_service" :sortable="true" header="Service" headerStyle="width: 250px; text-aling: left;"></Column>
<Column headerStyle="width: 8em">
<template #body >
<Button type="button" icon="pi pi-user" class="p-button-text" title="Détails"></Button>
<Button type="button" icon="pi pi-pencil" class="p-button-text" title="Modifier"></Button>
<Button type="button" icon="pi pi-trash" class="p-button-text" title="Supprimer"></Button>
</template>
</Column>
</DataTable>
</div>
<div class="button_gerer">
<button type="submit" class="boutton coin_vert" @click="cAjouter()">Ajouter un agent</button>
<button type="button" class="boutton coin_rouge" @click="cQuitter()">Abandon</button>
</div>
</div>
</template>
<style>
:root {
--gaucheP: 660px;
}
.principal_personnes {
height: var(--haut);
width: var(--large);
background-color:cornsilk;
}
.tableau {
width: 1000px;
margin: 0 auto;
}
.pi-user {
color:blue;
font-size: 1em;
}
.pi-pencil {
color:green;
font-size: 1em;
}
.pi-trash {
color:red;
font-size: 1em;
}
.p-datatable .p-datatable-tbody > tr > td {
text-align: left;
border: 1px solid #e9ecef;
border-width: 0 0 1px 0;
padding: 0rem 0rem;
}
h2 {
text-align:center;
}
.button_gerer {
display: grid;
grid-template: 30px / 130px 130px;
column-gap: 220px;  /* Firefox 63+ Chrome 84+ */
justify-content: stretch;
align-content: stretch;
padding-left: var(--gaucheP);
padding-top: 40px;
}
</style>
<script>
import DataTable from 'primevue/datatable';
import Column from 'primevue/column';
import Button from 'primevue/button';
import ('primeicons/primeicons.css');
import AgentService from '../../services/AgentService'
export default {
name: 'gererPersonnes',
beforeCreate() {
window.self.document.title = "gererPersonnes"; // modification du titre de l'onglet
document.getElementById('menuU').style.setProperty('display', 'none'); //menu non affiché
},
components : {
DataTable,
Column,
Button
},
data() {
return {
agents : null,
titre: 'Gérer les personnes',
}
},
methods : {
cQuitter() {
let a = confirm("Voulez quitter la gestion des personnes ?");
if (a == true) {
this.$router.push("/");
}
},
cAjouter() {
let routeUrl = this.$router.resolve({
path : "/agents/add_agent",
});
window.open(routeUrl.href, '_blank');
}
},
created() {
this.AgentService = new AgentService;
},
mounted () {
this.AgentService.getAgentServices().then(data => this.agents = data);
},
AgentService : null
}

//Centrage du formulaire de la page
var inHeight = window.innerHeight - 140;
var inWidth = window.innerWidth;
if (inWidth > 1680) {
inWidth = 1680;
}
var l = Math.floor((inWidth - 460 - 20)/2);
var padLeft = l + 'px';
var hauteur = inHeight + 'px';
var r = document.querySelector(':root');
r.style.setProperty('--gaucheP', padLeft);
r.style.setProperty('--haut', hauteur);
</script>

gererPersonnes调用的服务。vue: AgentService.js

import axios from "axios";
const URL = "/cote-s-rest/rest/agent"; 
const URL2 = "/cote-s-rest/rest/password/"; 
const URL3 = "/cote-s-rest/rest/agent/patronyme"; 
const URL4 = "/cote-s-rest/rest/agent/agentServ";
export default class AgentService {
insertAgent(agent) {
return axios.post(URL, agent).then(resp => {
return Promise.resolve(resp.data);
});
}
passNewAgent(agent) {
return axios.post(URL2, agent).then(resp => {
return Promise.resolve(resp.data);
});
}
getAgents() {
return axios.get(URL3).then(resp => {
return Promise.resolve(resp.data);
});
}
getAgentServices() {
return axios.get(URL4).then(resp => {
return Promise.resolve(resp.data);
});
}
}

AgentServ在Java中使用的类


package fr.gouv.finances.douane.cotes.dao.agent;
/**
* Classe de requete pour la liste "AgentServ"
* 
* @author Jean Paul Vandekerkhove
* @version 0.0.2.SNAPSHOT
*/
public class AgentServ {
//------------------------
// Attributs
//------------------------
private long id;
private String fullName;
private String acs;
private String nom_service;

//------------------------
// Constructeurs
//------------------------

/**
* Constructeur vide
*/
public AgentServ() {
super();
}
/**
* Constructeur complet
* 
* @param id
* @param fullName
* @param acs
* @param nom_service
*/
public AgentServ(long id, String fullName, String acs, String nom_service) {
super();
this.id = id;
this.fullName = fullName;
this.acs = acs;
this.nom_service = nom_service;
}
//------------------------
// Getters et Setters
//------------------------
/**
* Retourne l'identifiant de l'Agent
* @return id - identifiant de l'Agent
*/
public long getId() {
return id;
}

/**
* MaJ de l'Identifiant de l'Agent
* @param id - Identifiant de l'agent
*/
public void setId(long id) {
this.id = id;
}

/**
* Retourne l'identité complete de l'Agent
* @return fullName - nom-prénom de l'Agent
*/
public String getFullName() {
return fullName;
}

/**
* MaJ de l'identité complete de l'Agent
* @param fullName - nom-prénom de l'Agent
*/
public void setFullName(String fullName) {
this.fullName = fullName;
}

/**
* Retourne l'acs du service de l'Agent
* @return acs - acs du service de l'Agent
*/
public String getAcs() {
return acs;
}

/**
* MaJ de l'acs du service de l'Agent
* @param acs - acs du service de l'Agent
*/
public void setAcs(String acs) {
this.acs = acs;
}

/**
* Retourne le libellé du service de l'Agent
* @return nom_service - libellé du service de l'Agent
*/
public String getNom_service() {
return nom_service;
}

/**
* MaJ le libellé du service de l'Agent
* @param nom_service - libellé du service de l'Agent
*/
public void setNom_service(String nom_service) {
this.nom_service = nom_service;
}
}

列表方法在AgentDaoImpl.java中的使用


@SuppressWarnings("unchecked")
public List<AgentServ> listAgentService() {
String buf = null;
buf = "SELECT a.id AS id,(btrim(a.nom) || ' ' || btrim(a.prenom)) AS fullName, s.acs AS acs, s.libelle AS nom_service ";
buf += "FROM Agent a INNER JOIN Service s ON a.service = s ";
buf += "WHERE ((a.actif = true) OR ((a.actif = false) AND (a.dateSuppression >= CURRENT_DATE)))";
List<AgentServ> agents= em.createQuery(buf.toString()).getResultList();
return agents;
}

REST方法

package fr.gouv.finances.douane.cotes.rest.agent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.NoResultException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import fr.gouv.finances.douane.cotes.dao.agent.Agent;
import fr.gouv.finances.douane.cotes.dao.agent.AgentDAO;
import fr.gouv.finances.douane.cotes.dao.agent.AgentServ;
import fr.gouv.finances.douane.cotes.dao.agent.ListAgent;
/**
* Classe de ressources REST de la classe ejb Agent
*
* @author VANDEKERKHOVE
* @version 0.0.1.SNAPSHOT
*/
@Path("/agent")
@RequestScoped
public class AgentResourceRESTService {
@Inject
private Logger log;
@Inject
private Validator validator;
@Inject
private AgentDAO repository;
@GET
@Path("/agentServ")
@Produces("application/json; charset=UTF-8")
public List<AgentServ> ListAgentService() {
return (List<AgentServ>) repository.listAgentService();
}

rest项目的pom

project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>fr.gouv.finances.douane.cote-s</groupId>
<artifactId>cote-s</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>cote-s-rest</artifactId>
<version>${project.parent.version}</version>
<packaging>war</packaging>
<description>Modules Rest de Cote-S</description>

<dependencies>
<dependency>
<groupId>fr.gouv.finances.douane.cote-s</groupId>
<artifactId>cote-s-ejb</artifactId>
<version>${project.parent.version}</version>
<type>ejb</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.11.2.Final</version> <!-- $NO-MVN-MAN-VER$ -->   
<scope>provided</scope>
</dependency>

<!-- Tests -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>  
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>

您需要的Json实际上不是Json。这是你真正想要的:

[
{
"id":32,
"fullName":"DE BELLOUET Jag",
"acs":"1.3.13.3.",
"nom_service":"Cid polseruti sau"
},
{
"id":15,
"fullName":"NOURAUD Benjamin",
"acs":"1.3.13.3.",
"nom_service":"Cid polseruti sau"
}
]

重要的细节是用于表示对象或属性映射的{}字符。

这样说,您可能应该创建一个POJO来存储SQL请求的结果。比如:

public class Agent {
private Integer id;
private String fullName;
private String acs;
private String nomService;
// add some getters and setter here
}

也许从你的rest方法返回一个List<Agent>就足够了。如果没有,您可以查看jackson库,将List<Agent>序列化为json。

解决方案的第一部分实际上涉及到创建一个类(AgentServ)。

但是hibernate不喜欢我的请求,它必须把结果放在Object[]的列表中。

然后我们必须转换之前的结果,一步一步地将这些行包含在代理对象中,然后将其添加到列表中。

数据表现在返回请求的结果。

下面是最终代码。

public List<AgentServ> listAgentService() {
String buf = null;
buf = "SELECT a.id AS id,(btrim(a.nom) || ' ' || btrim(a.prenom)) AS fullName, s.acs AS acs, s.libelle AS nom_service ";
buf += "FROM Agent a INNER JOIN Service s ON a.service = s ";
buf += "WHERE ((a.actif = true) OR ((a.actif = false) AND (a.dateSuppression >= CURRENT_DATE)))";
List<Object[]> query = em.createQuery(buf.toString(), Object[].class).getResultList();
List<AgentServ> agents = new ArrayList<AgentServ>();

for(Object[] row : query) {
AgentServ agent = new AgentServ();
agent.setId((long) row[0]);
agent.setFullName((String) row[1]);
agent.setAcs((String) row[2]);
agent.setNom_service((String) row[3]);

agents.add(agent);
}

return agents;      
}

相关内容

最新更新