List of usage examples for com.google.gson JsonParser parse
@Deprecated public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
From source file:jsonSploit.java
/** * @param args the command line arguments *//* ww w. j av a 2s . com*/ public static void main(String[] args) { // TODO code application logic here Gson gson = new Gson(); System.out.println("OBTENER SOLO UN ARRAY DE CADENA JSON"); String myURL = "http://192.168.5.44/app_dev.php/cus/getaccount/50241109321.json"; System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) { urlConn.setReadTimeout(60 * 1000); } if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Exception while calling URL:" + myURL, e); } String jsonResult = sb.toString(); System.out.println(sb.toString()); System.out.println("\n\n--------------------JSON OBJECT DISPLAY----------------------------\n\n"); // convertimos el string a json Object JSONObject jsonObj = new JSONObject(jsonResult); // System.out.println("LA CADENA JSON CONVERTIDA EN OBJETO ES>"); System.out.println(jsonObj.toString()); System.out .println("\n\n--------------------- OBTENER ARRAYS DEL OBJETO JSON---------------------------\n\n"); JSONArray jsonArray = new JSONArray(); jsonArray.put(jsonObj); gson = new Gson(); Iterator x = jsonObj.keys(); while (x.hasNext()) { String key = (String) x.next(); jsonArray.put(jsonObj.get(key)); System.out.println(key); } System.out .println("\n\n--------------------- OBTENER ARRAYS DEL OBJETO JSON---------------------------\n\n"); JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject) jsonParser.parse(jsonResult); JsonArray jsonArr = jo.getAsJsonArray("data"); Gson googleJson = new Gson(); ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class); System.out.println("Listas existentes en data : " + jsonObjList.size()); System.out.println("los elementos de la lista son : " + jsonObjList.toString()); String dataResult = jsonObjList.toString(); System.out.println("\n\n--------------------- EL OBJETO JSON ARRAY---------------------------\n\n"); /// jsonArr.remove(1); String elemento1 = null; Iterator<JsonElement> nombreIterator = jsonArr.iterator(); JsonElement elemento = null; while (nombreIterator.hasNext()) { elemento = nombreIterator.next(); System.out.print(elemento + " \n"); elemento1 = elemento.toString(); System.out.println("El elemento 1 es " + elemento1); } cData data = gson.fromJson(elemento1, cData.class); // Account account = gson.fromJson(jo, Account.class); if (data.getAccount().getFirst_name() == null) { System.out.println("Error"); } else { System.out.print(data.getAccount().getFirst_name()); } // System.out.println(data.acount.getNumber()); System.out.println("\n\n--------------------- OBTENEMOS EL OBJETO ACCOUNT---------------------------\n\n"); //JsonObject jObject = (JsonObject) jsonParser.parse(elemento1); // System.out.println(jObject.getAsJsonPrimitive("number")); }
From source file:CBDocUtil.java
String prettyPrint(String uglyJSONString) { String prettyJsonString = uglyJSONString; try {/*from ww w .j a v a2 s .c o m*/ Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJSONString); prettyJsonString = gson.toJson(je); } catch (Exception e1) { logger.log(java.util.logging.Level.INFO, "Excepton {0} {1} ", new Object[] { e1.getMessage(), getPrintStackTrace(e1) }); } return prettyJsonString; }
From source file:RawCollectionsExample.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { Gson gson = new Gson(); Collection collection = new ArrayList(); collection.add("hello"); collection.add(5);// w w w .j a v a 2s . c om collection.add(new Event("GREETINGS", "guest")); String json = gson.toJson(collection); System.out.println("Using Gson.toJson() on a raw collection: " + json); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonArray(); String message = gson.fromJson(array.get(0), String.class); int number = gson.fromJson(array.get(1), int.class); Event event = gson.fromJson(array.get(2), Event.class); System.out.printf("Using Gson.fromJson() to get: %s, %d, %s", message, number, event); }
From source file:JsonConversion.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "mkyong.com"); obj.put("age", new Integer(100)); JSONArray list = new JSONArray(); JSONObject obj1 = new JSONObject(); obj1.put("1", "msg 1"); list.add(obj1);/* www . j a v a2 s. c om*/ JSONObject obj2 = new JSONObject(); obj2.put("2", "msg 2"); list.add(obj2); JSONObject obj3 = new JSONObject(); obj3.put("3", "msg 3"); list.add(obj3); obj.put("messages", list); String jsonStr1 = obj.toJSONString(); String jsonStr = "{\"endLong\"=\"78.3769397\", \"endTime\"=\"16:48:28\", \"endLat\"=\"17.4466785\", \"tripData\"=\"[{Longitude:78.3769388,Latitude:17.4466736},{Longitude:78.3769388,Latitude:17.4466736}]\", \"endLoc\"=\"Deloitte Dr,Hyderabad,India500081\",\"emailId\"=\"user15@gmail.com\", \"tripId\"=\"30\"}"; /*try { FileWriter file = new FileWriter("c:\\test.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); }*/ System.out.print(obj); JsonParser parser = new JsonParser(); int firstIndex = jsonStr.indexOf("["); int lastIndex = jsonStr.indexOf("]"); StringBuilder sb = new StringBuilder(jsonStr); sb.deleteCharAt(firstIndex - 1); sb.deleteCharAt(lastIndex); String str1 = sb.toString(); Object objFinal = parser.parse(str1); JsonObject jsonObject = (JsonObject) objFinal; String name = String.valueOf(jsonObject.get("endLong")); System.out.println(name); Map msg = (Map) jsonObject.get("tripData"); }
From source file:ProcessRequest.java
public void updateProduct(JSONObject recordObject) throws JSONException, SQLException { String valuesPortion = ""; recordObject.remove(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); String productID = null;// w w w.ja va 2s. c o m //construct the SQL query from the JSON elements. //the valuesPortion String will contain the values we want to update in the record { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += entry.getKey() + "="; valuesPortion += "'" + value.getAsString() + "'"; if (entry.getKey().equals(DbSingleton.ProductSchema.COLUMN_ID)) { productID = value.getAsString(); } } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += value.getAsString(); } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString())); if (value.getAsBoolean()) { valuesPortion += entry.getKey() + "="; valuesPortion += "1"; } else { valuesPortion += entry.getKey() + "="; valuesPortion += "0"; } } } } } //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")"; if (productID == null) { throw new SQLException("no customer id found!"); } String updateQuery = "UPDATE " + DbSingleton.PRODUCT_TABLE_NAME + " SET " + valuesPortion + " WHERE " + DbSingleton.ProductSchema.COLUMN_ID + "='" + productID + "'"; //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID"; executeQuery(updateQuery); }
From source file:ProcessRequest.java
public void newProduct(JSONObject recordObject) throws JSONException, SQLException { String valuesPortion = ""; String columnsPortion = ""; recordObject.remove(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); {// w ww .j a v a2 s . c o m Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { if (!columnsPortion.equals("")) { columnsPortion += ","; } columnsPortion += entry.getKey(); } } } //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += new Long(value.getAsLong()).toString(); } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } if (value.getAsBoolean()) valuesPortion += "1"; else valuesPortion += "0"; } } } } String insertQuery = "INSERT INTO " + DbSingleton.PRODUCT_TABLE_NAME + "(" + columnsPortion + ") VALUES (" + valuesPortion + ")"; String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.PRODUCT_TABLE_NAME + "') AS ProductID"; executeQuery(insertQuery); }
From source file:ProcessRequest.java
public void updateCustomer(JSONObject recordObject) throws JSONException, SQLException { String valuesPortion = ""; String dob = null;//w w w . j a v a 2 s .c om try { dob = mDateFormat .format(new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_DOB))); } catch (JSONException jsone) { dob = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_DOB); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_DOB, dob); } String issueDate = null; try { issueDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE))); } catch (JSONException jsone) { issueDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE, issueDate); } String expirationDate = null; try { expirationDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE))); } catch (JSONException jsone) { expirationDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE, expirationDate); } recordObject.remove(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); String customerID = null; //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += entry.getKey() + "="; valuesPortion += "'" + value.getAsString() + "'"; if (entry.getKey().equals(DbSingleton.CustomerSchema.COLUMN_ID)) { customerID = value.getAsString(); } } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += value.getAsString(); } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString())); if (value.getAsBoolean()) { valuesPortion += entry.getKey() + "="; valuesPortion += "1"; } else { valuesPortion += entry.getKey() + "="; valuesPortion += "0"; } } } } } //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")"; if (customerID == null) { throw new SQLException("no customer id found!"); } String updateQuery = "UPDATE " + DbSingleton.CUSTOMER_TABLE_NAME + " SET " + valuesPortion + " WHERE " + DbSingleton.CustomerSchema.COLUMN_ID + "='" + customerID + "'"; //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID"; executeQuery(updateQuery); }
From source file:ProcessRequest.java
public void newCustomer(JSONObject recordObject) throws JSONException, SQLException { String valuesPortion = ""; String columnsPortion = ""; String dob = null;//from w w w .ja v a 2 s . c o m try { dob = mDateFormat .format(new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_DOB))); } catch (JSONException jsone) { dob = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_DOB); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_DOB, dob); } String issueDate = null; try { issueDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE))); } catch (JSONException jsone) { issueDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE, issueDate); } String expirationDate = null; try { expirationDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE))); } catch (JSONException jsone) { expirationDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE); } finally { recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE, expirationDate); } recordObject.remove(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { if (!columnsPortion.equals("")) { columnsPortion += ","; } columnsPortion += entry.getKey(); } } } //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += new Double(value.getAsDouble()).toString(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } valuesPortion += new Long(value.getAsLong()).toString(); } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } if (value.getAsBoolean()) valuesPortion += "1"; else valuesPortion += "0"; } } } } String insertQuery = "INSERT INTO " + DbSingleton.CUSTOMER_TABLE_NAME + "(" + columnsPortion + ") VALUES (" + valuesPortion + ")"; //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.CUSTOMER_TABLE_NAME+"') AS CustomerID"; //String customerIsHaulerQuery = "SELECT * FROM "+DbSingleton.CUSTOMER_TABLE_NAME+" " executeQuery(insertQuery); /* ResultSet identResultSet = executeQuery(identQuery); identResultSet.first(); //System.out.println(identResultSet.getRow()); int customerID = identResultSet.getInt(1); //System.out.println(ticketID); return customerID; */ }
From source file:ProcessRequest.java
public void updateTicket(JSONObject recordObject) throws SQLException, JSONException { //check for ticket existence //check for ticket detail existence String valuesPortion = ""; String ticketDate = null;// www. j av a 2 s. co m try { ticketDate = mDateFormat .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TICKET_DATE))); } catch (JSONException jsone) { ticketDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TICKET_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_DATE, ticketDate); } String invoiceDate = null; try { invoiceDate = mDateFormat .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE))); } catch (JSONException jsone) { invoiceDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE, invoiceDate); } String voucherRedeemableDate = null; try { voucherRedeemableDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE))); } catch (JSONException jsone) { voucherRedeemableDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE, voucherRedeemableDate); } String voucherExpirationDate = null; try { voucherExpirationDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE))); } catch (JSONException jsone) { voucherExpirationDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE, voucherExpirationDate); } String paymentDate = null; try { paymentDate = mDateFormat .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE))); } catch (JSONException jsone) { paymentDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE, paymentDate); } String trailerLastTareDate = null; try { trailerLastTareDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE))); } catch (JSONException jsone) { trailerLastTareDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE, trailerLastTareDate); } String truckLastTareDate = null; try { truckLastTareDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE))); } catch (JSONException jsone) { truckLastTareDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE); } finally { recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE, truckLastTareDate); } recordObject.remove(DbSingleton.TicketSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.TicketSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER); //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber()); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); Integer ticketID = null; //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += entry.getKey() + "="; valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += new Double(value.getAsDouble()).toString(); } else { //String ticketNumber = new Long(value.getAsLong()).toString(); Long val = new Long(value.getAsLong()); if (entry.getKey().equals(DbSingleton.TicketSchema.COLUMN_TICKET_ID)) { ticketID = val.intValue(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += val.toString(); } } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString())); if (value.getAsBoolean()) { valuesPortion += entry.getKey() + "="; valuesPortion += "1"; } else { valuesPortion += entry.getKey() + "="; valuesPortion += "0"; } } } } } //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")"; if (ticketID == null) { throw new SQLException("no ticket id found!"); } String updateQuery = "UPDATE " + DbSingleton.TICKET_TABLE_NAME + " SET " + valuesPortion + " WHERE " + DbSingleton.TicketSchema.COLUMN_TICKET_ID + "=" + ticketID; //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID"; executeQuery(updateQuery); }
From source file:ProcessRequest.java
public void updateTicketDetail(Integer ticketID, JSONObject recordObject) throws SQLException, JSONException { //check for ticket detail existence String valuesPortion = ""; recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_LAST_MODIFIED); recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date())); recordObject.remove("TMSRowGUID"); String grossDate = null;//from w w w . j a v a 2s.co m try { grossDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT))); } catch (JSONException jsone) { grossDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT, grossDate); } String grossALTDate = null; try { grossALTDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT))); } catch (JSONException jsone) { grossALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT, grossALTDate); } String tareDate = null; try { tareDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT))); } catch (JSONException jsone) { tareDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT, tareDate); } String tareALTDate = null; try { tareALTDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT))); } catch (JSONException jsone) { tareALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT, tareALTDate); } String deductDate = null; try { deductDate = mDateFormat.format( new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT))); } catch (JSONException jsone) { deductDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT, deductDate); } String deductALTDate = null; try { deductALTDate = mDateFormat.format(new java.util.Date( recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT))); } catch (JSONException jsone) { deductALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT); } finally { recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT, deductALTDate); } //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER); //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber()); //get column names JsonParser parser = new JsonParser(); JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet(); Integer ticketDetailID = null; //get values { Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); if (entry.getKey().equals("metadata")) { //do nothing } else { JsonPrimitive value = entry.getValue().getAsJsonPrimitive(); //System.out.println(entry.getKey()+" : "+value.getAsString()); if (value.isString()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'"); valuesPortion += entry.getKey() + "="; valuesPortion += "'" + value.getAsString() + "'"; } else if (value.isNumber()) { if (value.getAsString().contains(".")) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString())); valuesPortion += entry.getKey() + "="; valuesPortion += new Double(value.getAsDouble()).toString(); } else { Long val = new Long(value.getAsLong()); if (entry.getKey().equals(DbSingleton.TicketDetailSchema.COLUMN_TICKET_DETAIL_ID)) { ticketDetailID = val.intValue(); } else { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString())); valuesPortion += entry.getKey() + "="; //String ticketNumber = new Long(value.getAsLong()).toString(); valuesPortion += val.toString(); } } } else if (value.isBoolean()) { if (!valuesPortion.equals("")) { valuesPortion += ","; } //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString())); if (value.getAsBoolean()) { valuesPortion += entry.getKey() + "="; valuesPortion += "1"; } else { valuesPortion += entry.getKey() + "="; valuesPortion += "0"; } } } } } //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")"; if (ticketDetailID == null) { throw new SQLException("no ticket detail id found!"); } String updateQuery = "UPDATE " + DbSingleton.TICKET_DETAIL_TABLE_NAME + " SET " + valuesPortion + " WHERE " + DbSingleton.TicketDetailSchema.COLUMN_TICKET_DETAIL_ID + "=" + ticketDetailID; //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID"; executeQuery(updateQuery); }