private AchievementBean[] parseAchievementsJSon(String json) { JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(Category.class)); JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(Difficulty.class)); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); jsonConfig.setRootClass(AchievementBean.class); return (AchievementBean[]) JSONSerializer.toJava(jsonArray, jsonConfig); }
protected JSONObject representationToJSONObject(Representation representation) throws ResourceException { String posted; try { posted = representation.getText(); } catch (IOException e) { throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "IOException on input."); } if (!JSONUtils.mayBeJSON(posted)) { throw error(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE, "Please PUT in JSON format."); } JSONObject json; try { JSON input = JSONSerializer.toJSON(posted); if (!input.isArray()) { json = (JSONObject) input; } else { throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "Please send a proper JSON object."); } } catch (JSONException e) { throw error(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE, "JSON format required."); } return json; }
public void testReadArray() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JsonLibModule()); JSONArray array = mapper.readValue("[null, 13, false, 1.25, \"abc\", {\"a\":13}, [ ] ]", JSONArray.class); assertEquals(7, array.size()); assertTrue(JSONUtils.isNull(array.get(0))); assertEquals(13, array.getInt(1)); assertFalse(array.getBoolean(2)); assertEquals(Double.valueOf(1.25), array.getDouble(3)); assertEquals("abc", array.getString(4)); JSONObject ob = array.getJSONObject(5); assertEquals(1, ob.size()); assertEquals(13, ob.getInt("a")); JSONArray array2 = array.getJSONArray(6); assertEquals(0, array2.size()); }
public void testReadObject() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JsonLibModule()); JSONObject ob = mapper.readValue( "{\"a\":{\"b\":3}, \"c\":[9, -4], \"d\":null, \"e\":true}", JSONObject.class); assertEquals(4, ob.size()); JSONObject ob2 = ob.getJSONObject("a"); assertEquals(1, ob2.size()); assertEquals(3, ob2.getInt("b")); JSONArray array = ob.getJSONArray("c"); assertEquals(2, array.size()); assertEquals(9, array.getInt(0)); assertEquals(-4, array.getInt(1)); assertTrue(JSONUtils.isNull(ob.get("d"))); assertTrue(ob.getBoolean("e")); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { log.info("API request received path=" + request.getPathInfo()); String[] params = request.getPathInfo().split("/"); Long networkId = new Long(params[1]); log.info("networkId=" + networkId); // EntityManager em = HibernateUtil.getEntityManager(); // HANetwork network = em.find(HANetwork.class, networkId); HANetwork network = HANetwork.getInstance(networkId); String methodClass = "ie.wombat.ha.api.method." + params[2]; Method method; try { method = (Method) Class.forName(methodClass).newInstance(); } catch (InstantiationException e) { returnError(response, METHOD_ERROR); return; } catch (IllegalAccessException e) { returnError(response, METHOD_ERROR); return; } catch (ClassNotFoundException e) { returnError(response, METHOD_NOT_FOUND); return; } log.info("method=" + method); response.setContentType("application/json"); MethodResponse mresp = method.invokeMethod(network, params); response .getOutputStream() .print("{\"status\":0,\"result\":" + JSONUtils.quote(mresp.getResponse()) + "}"); }
private String addVClassDataToResultsList( WebappDaoFactory wadf, VClass vcw, int position, String ontologyUri, int counter) { String tempString = ""; if (ontologyUri == null || ((vcw.getNamespace() != null) && (vcw.getNamespace().equals(ontologyUri)))) { // first if statement ensures that the first class begins with correct format if (counter < 1 && position < 1) { tempString += "{ \"name\": "; } else if (position == previous_posn) { tempString += "}, { \"name\": "; } else if (position > previous_posn) { tempString += " { \"name\": "; } else if (position < previous_posn) { tempString += "}, { \"name\": "; } try { tempString += JSONUtils.quote( "<a href='vclassEdit?uri=" + URLEncoder.encode(vcw.getURI(), "UTF-8") + "'>" + vcw.getPickListName() + "</a>") + ", "; } catch (Exception e) { tempString += JSONUtils.quote(((vcw.getPickListName() == null) ? "" : vcw.getPickListName())) + ", "; } String shortDef = ((vcw.getShortDef() == null) ? "" : vcw.getShortDef()); tempString += "\"data\": { \"shortDef\": " + JSONUtils.quote(shortDef) + ", "; // Get group name if it exists VClassGroupDao groupDao = wadf.getVClassGroupDao(); String groupURI = vcw.getGroupURI(); String groupName = null; VClassGroup classGroup = null; if (groupURI != null) { classGroup = groupDao.getGroupByURI(groupURI); if (classGroup != null) { groupName = classGroup.getPublicName(); } } tempString += "\"classGroup\": " + JSONUtils.quote((groupName == null) ? "" : groupName) + ", "; // Get ontology name OntologyDao ontDao = wadf.getOntologyDao(); String ontName = vcw.getNamespace(); Ontology ont = ontDao.getOntologyByURI(ontName); if (ont != null && ont.getName() != null) { ontName = ont.getName(); } tempString += "\"ontology\": " + JSONUtils.quote((ontName == null) ? "" : ontName) + "}, \"children\": ["; previous_posn = position; } return tempString; }
private static void setDataFormat2JAVA() { // 设定日期转换格式 JSONUtils.getMorpherRegistry() .registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"})); }
public static String json(Object object) { if (JSONUtils.isArray(object)) return JSONArray.fromObject(object).toString(); if (JSONUtils.isObject(object)) return JSONObject.fromObject(object).toString(); return object.toString(); }
/** * 获取自动回复规则 * * @param accessToken * @return */ public static AutoReplyInfoRule getAutoReplyInfoRule(String accessToken) throws WexinReqException { AutoReplyRuleGet arr = new AutoReplyRuleGet(); arr.setAccess_token(accessToken); JSONObject result = WeiXinReqService.getInstance().doWeinxinReqJson(arr); Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE); AutoReplyInfoRule autoReplyInfoRule = (AutoReplyInfoRule) JSONObject.toBean( result, new CustomJsonConfig(AutoReplyInfoRule.class, "keyword_autoreply_info")); JSONObject keywordAutoReplyInfoJsonObj = result.getJSONObject("keyword_autoreply_info"); if (keywordAutoReplyInfoJsonObj != null && !JSONUtils.isNull(keywordAutoReplyInfoJsonObj)) { /** 关键词自动回复的信息 */ JSONArray keywordAutoReplyInfos = keywordAutoReplyInfoJsonObj.getJSONArray("list"); if (keywordAutoReplyInfos != null) { List<KeyWordAutoReplyInfo> listKeyWordAutoReplyInfo = new ArrayList<KeyWordAutoReplyInfo>(); for (int i = 0; i < keywordAutoReplyInfos.size(); i++) { KeyWordAutoReplyInfo keyWordAutoReplyInfo = (KeyWordAutoReplyInfo) JSONObject.toBean( keywordAutoReplyInfos.getJSONObject(i), new CustomJsonConfig( KeyWordAutoReplyInfo.class, new String[] {"keyword_list_info", "reply_list_info"})); /** 处理关键词列表 */ JSONArray keywordListInfos = keywordAutoReplyInfos.getJSONObject(i).getJSONArray("keyword_list_info"); if (keywordListInfos != null) { List<KeywordListInfo> listKeywordListInfo = new ArrayList<KeywordListInfo>(); for (int j = 0; j < keywordListInfos.size(); j++) { KeywordListInfo keywordListInfo = (KeywordListInfo) JSONObject.toBean(keywordListInfos.getJSONObject(j), KeywordListInfo.class); listKeywordListInfo.add(keywordListInfo); } keyWordAutoReplyInfo.setKeyword_list_info(listKeywordListInfo); } /** 处理关键字回复信息 */ JSONArray replyListInfos = keywordAutoReplyInfos.getJSONObject(i).getJSONArray("reply_list_info"); if (replyListInfos != null) { List<ReplyListInfo> listReplyListInfo = new ArrayList<ReplyListInfo>(); for (int j = 0; j < replyListInfos.size(); j++) { ReplyListInfo replyListInfo = (ReplyListInfo) JSONObject.toBean( keywordListInfos.getJSONObject(j), new CustomJsonConfig(ReplyListInfo.class, "news_info")); /** 处理关键字回复相关图文消息 */ JSONObject newsInfoJsonObj = replyListInfos.getJSONObject(j).getJSONObject("news_info"); if (newsInfoJsonObj != null && !JSONUtils.isNull(newsInfoJsonObj)) { JSONArray newsInfos = newsInfoJsonObj.getJSONArray("list"); List<WxArticleConfig> listNewsInfo = new ArrayList<WxArticleConfig>(); for (int k = 0; k < newsInfos.size(); k++) { WxArticleConfig wxArticleConfig = (WxArticleConfig) JSONObject.toBean(newsInfos.getJSONObject(k), WxArticleConfig.class); listNewsInfo.add(wxArticleConfig); } replyListInfo.setNews_info(listNewsInfo); } listReplyListInfo.add(replyListInfo); } keyWordAutoReplyInfo.setReply_list_info(listReplyListInfo); } listKeyWordAutoReplyInfo.add(keyWordAutoReplyInfo); } autoReplyInfoRule.setKeyword_autoreply_info(listKeyWordAutoReplyInfo); } } return autoReplyInfoRule; }