public void testcreateblog2() { System.out.println("Test Case to Create a Blog"); Resource resource1 = client.resource(url); System.out.println("URL: " + url); JSONObject sendobject = new JSONObject(); try { sendobject.put("subject", "sjsu_cmpe202"); sendobject.put("description", "XML"); sendobject.put("userid", "Shaunak"); sendobject.put("timestamp", "11:53"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } String jsonStringObj = sendobject.toString(); System.out.println("Create : " + jsonStringObj); ClientResponse created = resource1 .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .post(jsonStringObj); System.out.println(created.getStatusCode()); System.out.println(created.getEntity(String.class)); }
private void initializeJSON() throws JSONException { rulesArray = new JSONArray(); jsonGroupObject.put("rules", rulesArray); JSONObject modelInfo = new JSONObject(); modelArray = new JSONArray(); modelInfo.put("modelgroups", modelArray); jsonGroupObject.put("groups", modelInfo); }
private void setJsonTree(List<String> path, JSONArray parent, GadgetSpec gadgetSpec) throws JSONException { String tempId = "root"; // Temp variable to keep track of parent ID. for (int i = 0; i < path.size(); i++) { JSONObject newNode = new JSONObject(); if (!this.isNodeExisting(path.get(i), parent)) { // If node is a GadgetSpec (last in path) if (i == path.size() - 1) { newNode.put("name", StringUtils.capitalize(gadgetSpec.getTitle())); newNode.put("hasChildren", false); newNode.put("isDefault", gadgetSpec.isDefault()); newNode.put("id", gadgetSpec.getId()); } // Id node is a unique folder else { newNode.put("name", StringUtils.capitalize(path.get(i))); newNode.put("hasChildren", true); newNode.put("isDefault", false); newNode.put("id", getFolderId(path.get(i))); } newNode.put("parent", tempId); parent.put(newNode); } tempId = getFolderId(path.get(i)); } }
/** * Gets the json. * * @param AssignedToUser the assigned to user * @param subjectId the subject id * @param tittle the tittle * @param content the content * @return the json * @throws Exception the exception */ private String getJSON(String AssignedToUser, String subjectId, String tittle, String content) throws Exception { JSONObject json1 = new JSONObject(); json1.put("subjectId", subjectId); json1.put("assignedUserId", AssignedToUser); json1.put("title", tittle); JSONArray parameter = new JSONArray(); parameter.add(json1); parameter.add(content); JSONObject json = new JSONObject(); json.put("parameters", parameter); return json.toString(); }
public JSONObject toJSON() { JSONObject json = new JSONObject(); JSONObject LDPLJSON = new JSONObject(); JSONObject GPJSON = new JSONObject(); JSONObject optJSON = new JSONObject(); try { double[] params = getParams(); LDPLJSON .put("n", params[0]) .put("A", params[1]) .put("fa", params[2]) .put("fb", params[3]) .put(HDIFF, hDiff); GPJSON.put("lengthes", new Double[] {lengthes[0], lengthes[1], lengthes[2]}); GPJSON .put("sigmaP", stdev) .put("sigmaN", sigmaN) .put("useMask", gpLDPL.getUseMask() ? 1 : 0) .put("constVar", gpLDPL.getOptConstVar()); optJSON.put("optimizeHyperParameters", doHyperParameterOptimize ? 1 : 0); json.put(LDPLParameters, LDPLJSON); json.put(GPParameters, GPJSON); json.put(OPTIONS, optJSON); } catch (JSONException e) { e.printStackTrace(); } return json; }
protected Object parseBean( JSONObject jsonObj, Class<?> beanClazz, ValidationException validationException) throws IntrospectionException, IllegalAccessException, InstantiationException, JSONException, InvocationTargetException, ParseException { if (beanClazz == null) { throw new AJAXParserException("No specific class type."); } if (jsonObj == null) { return null; } BeanInfo info = Introspector.getBeanInfo(beanClazz); PropertyDescriptor[] propDescriptors = info.getPropertyDescriptors(); Object instance = beanClazz.newInstance(); for (PropertyDescriptor property : propDescriptors) { String name = property.getName(); Class<?> propClazz = property.getPropertyType(); if (Class.class.equals(propClazz)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } if ((propClazz.equals(java.sql.Date.class) || propClazz.equals(java.sql.Time.class) || propClazz.equals(java.sql.Timestamp.class) || propClazz.equals(java.util.Date.class)) && jsonObj.containsKey(name)) { Object tempValue = jsonObj.get(name); if (tempValue != null && StringUtil.isEmpty(tempValue.toString())) { tempValue = null; jsonObj.put(name, tempValue); } } if (validateValue(beanClazz, name, jsonObj, validationException) && jsonObj.containsKey(name)) { Object nestObj = null; if (isServiceBeanType(propClazz)) { JSONObject jsonObjPropValue = jsonObj.getJSONObject(name); nestObj = parseBean(jsonObjPropValue, propClazz, validationException); } else if (isBasicType(propClazz)) { Object jsonObjPropValue = jsonObj.get(name); nestObj = parseBasic(jsonObjPropValue == null ? null : jsonObjPropValue.toString(), propClazz); } else if (propClazz.isArray()) { if (jsonObj.get(name) == null) { continue; } JSONArray jsonArrPropValue = jsonObj.getJSONArray(name); nestObj = parseArray(jsonArrPropValue, propClazz.getComponentType(), validationException); } else if (List.class.isAssignableFrom(propClazz)) { if (jsonObj.get(name) == null) { continue; } Type genericType = property.getReadMethod().getGenericReturnType(); JSONArray jsonArrPropValue = jsonObj.getJSONArray(name); nestObj = parseList(jsonArrPropValue, propClazz, genericType, validationException); } else if (Map.class.isAssignableFrom(propClazz)) { Type genericType = property.getReadMethod().getGenericReturnType(); JSONObject jsonObjPropValue = jsonObj.getJSONObject(name); nestObj = parseMap(jsonObjPropValue, propClazz, genericType, validationException); } else if (java.lang.Object.class.equals(propClazz)) { nestObj = jsonObj.getJSONObject(name); } else { nestObj = null; } setter.invoke(instance, new Object[] {nestObj}); } } return instance; }