Example usage for javax.json.stream JsonParser next

List of usage examples for javax.json.stream JsonParser next

Introduction

In this page you can find the example usage for javax.json.stream JsonParser next.

Prototype

Event next();

Source Link

Document

Returns the event for the next parsing state.

Usage

From source file:com.assignment4.productdetails.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)//from  ww w  .j  av  a2  s . co m
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String string) throws SQLException {
    JsonParser parse = Json.createParser(new StringReader(string));
    Map<String, String> pdtmap = new LinkedHashMap<String, String>();
    String key = "";
    String value = "";

    while (parse.hasNext()) {
        JsonParser.Event event = parse.next();
        switch (event) {
        case KEY_NAME:
            key = parse.getString();
            break;
        case VALUE_STRING:
            value = parse.getString();
            pdtmap.put(key, value);
            break;
        case VALUE_NUMBER:
            value = parse.getString();
            pdtmap.put(key, value);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not able to  connect";
    } else {
        String query = "INSERT INTO products (product_id,name,decription,quantity) VALUES (?,?,?)";
        PreparedStatement prestmt = conn.prepareStatement(query);
        prestmt.setInt(1, Integer.parseInt(pdtmap.get("product_id")));
        prestmt.setString(2, pdtmap.get("name"));
        prestmt.setString(3, pdtmap.get("description"));
        prestmt.setInt(4, Integer.parseInt(pdtmap.get("quantity")));
        prestmt.executeUpdate();
        return " the row has been inserted into the database";
    }

}

From source file:com.seniorproject.semanticweb.services.WebServices.java

public String generateSparqlForAddProperty(String category, String property, String selectedValues) {
    String iri = getIRI(category);
    String queryString = "SELECT ?value ?label WHERE { ";
    queryString += "?subject rdf:type " + iri + " . ";
    if (selectedValues.length() > 0) {
        JsonParser parser = Json.createParser(new StringReader(selectedValues));

        Event event = parser.next();// START_OBJECT
        while ((event = parser.next()) != Event.END_OBJECT) {
            if (parser.getString().substring(0, 2).equalsIgnoreCase("is")) {

                String[] parts = parser.getString().split(" ");
                event = parser.next();/*from www.  j a va2 s. com*/
                queryString += convertToNoPrefix(parser.getString()) + " " + parts[1] + " ?subject .";
            } else {
                queryString += "?subject " + parser.getString() + " ";
                event = parser.next();
                queryString += convertToNoPrefix(parser.getString()) + ". ";
            }
        }
    }
    if (property.substring(0, 2).equalsIgnoreCase("is")) {
        String[] parts = property.split(" ");
        queryString += "?value " + parts[1] + " ?subject . ";
    } else {
        queryString += "?subject " + property + " ?value . ";
    }
    queryString += "OPTIONAL {?value rdfs:label ?label. }} ";
    return queryString;
}