如何在java中创建JSON解析脚本
我使用netbeans 8和ApacheTomcat 7,我需要在restful web服务中创建一个脚本,以便在web页面中生成结果,而不是restful测试web页面,这意味着解析。我怎么做?请帮帮我 这是连接类如何在java中创建JSON解析脚本,java,json,Java,Json,我使用netbeans 8和ApacheTomcat 7,我需要在restful web服务中创建一个脚本,以便在web页面中生成结果,而不是restful测试web页面,这意味着解析。我怎么做?请帮帮我 这是连接类 package entities; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; im
package entities;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class PatientCnx {
public static Connection connection = null;
public static PreparedStatement ptmt = null;
public static ResultSet resultSet = null;
private static Connection getConnection() throws SQLException {
connection = ConnectionFactory.getInstance().getConnection();
return connection;
}
public static List<Patient> findByPatientSexe(String c) {
String sql = ""
+ "SELECT * FROM patient WHERE sexe = ? ";
Connection conn = null;
List<Patient> patientList = new ArrayList<Patient>();
try {
connection = PatientCnx.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, c);
ResultSet resultSet = ps.executeQuery();
while(resultSet.next()) {
Patient p = new Patient();
p.setIdpatient(resultSet.getInt(1));
p.setNom(resultSet.getString(2));
p.setPrenom(resultSet.getString(4));
patientList.add(p);
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return patientList;
}
}
这是netbeans在我创建RESTfull Web服务时创建的genericressource
“@Pathgeneric
公共类通用资源{
@Context
private UriInfo context;
/**
* Creates a new instance of GenericResource
*/
public GenericResource() {
}
/**
* Retrieves representation of an instance of entities.GenericResource
* @return an instance of java.lang.String
*/
@GET
@Produces("application/json")
public String getJson() {
//TODO return proper representation object
throw new UnsupportedOperationException();
}
/**
* PUT method for updating or creating an instance of GenericResource
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes("application/json")
public void putJson(String content) {
}
@GET
@Path("{Sexe}")
@Produces({ "application/json"})
public List<Patient> find(@PathParam("Sexe") String c) {
List<Patient> p = PatientCnx.findByPatientSexe(c);
if (p == null)
return p;
return p;
}
}
`你可以上一节课。例如:
public class JsonParse {
private static final String filePath = "jsonFile.json"; //your object URL
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
String name = (String) jsonObject.get("name");
System.out.println("My name is " + name);
}catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
或者使用一些lib在rest中解析它。例如Jackson或Jersey有许多用于JSON解析的lib。最受欢迎的似乎是杰克逊,最标准化的可能是JSR-353。有很多参赛者是最快的:我不得不承认我不理解你的示例代码和描述之间的联系。可能您不需要一个json解析器,但是一个完整的rest客户机呢?我在使用netbeans,我用genericRessource类创建了一个restfull web服务由netbeans自己创建我上面写的代码Continent sql请求我的问题是当我测试Restful web服务时,它给了我一个接口,我真正需要的是没有我想要的接口的结果像这样[{idpatient:21,nom:xxxx,prenom:xxxxx},{idpatient:19,nom:yyyyyy,prenom:yyyyyyyy}],或者使用一些libs在rest中解析它。例如Jackson或Jersey.|因此您可以使用更多示例:Jackson是JAVA的json转换器。jackson jax rs称之为Jersey的旧版本。