如何在java中创建JSON解析脚本

时间:2014-11-05 13:51:23

标签: java json

我使用netbeans 8和apache tomcat 7,我需要在一个宁静的Web服务中创建一个脚本,以便将结果放在一个网页中,而不是安静的测试网页,这意味着解析。我该怎么做?请帮帮我。

这是连接类

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;

    }
}

这是我在创建RESTfull Web服务时由netbeans创建的genericressource     “@Path( “通用”) 公共类GenericResource {

@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;

} } `

1 个答案:

答案 0 :(得分:-1)

你可以上课。例如:

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();
        }
    }
}

或者使用一些libs来解析它。例如&#34; Jackson&#34;或&#34;泽西岛&#34;