本文整理匯總了Java中net.sf.json.JSONException類的典型用法代碼示例。如果您正苦於以下問題:Java JSONException類的具體用法?Java JSONException怎麽用?Java JSONException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JSONException類屬於net.sf.json包,在下文中一共展示了JSONException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: extractErrorMessage
import net.sf.json.JSONException; //導入依賴的package包/類
@Override
protected String extractErrorMessage(String response) {
if (response != null && !response.isEmpty()) {
try {
JSON jsonResponse = JSONSerializer.toJSON(response, new JsonConfig());
if (jsonResponse instanceof JSONObject) {
JSONObject object = (JSONObject) jsonResponse;
JSONObject errorObj = object.getJSONObject("error");
if (errorObj.containsKey("message")) {
return errorObj.getString("message");
}
}
} catch (JSONException ex) {
log.debug("Cannot parse JSON error response: " + response);
}
}
return response;
}
示例2: getAccessToken
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* 獲取access_token
*
* @return
* @deprecated
*/
public static Token getAccessToken(String appid, String appsecret) {
Token accessToken = null;
String requestUrl = token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
logger.info("獲取access_token的請求url:{}", requestUrl);
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);
// 如果請求成功
if (null != jsonObject) {
try {
accessToken = new Token();
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
accessToken = null;
// 獲取token失敗
logger.error("獲取token失敗 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return accessToken;
}
示例3: jsonString2Map
import net.sf.json.JSONException; //導入依賴的package包/類
public static Map<String, String> jsonString2Map(String jsonStr) {
JSONObject json_obj = null;
try {
json_obj = JSONObject.fromObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
}
if (json_obj == null)
return null;
Set<String> obj_keys = json_obj.keySet();
if (obj_keys == null)
return null;
Map<String, String> map = new HashMap<String, String>();
for (String item_key : obj_keys) {
map.put(item_key, json_obj.getString(item_key));
}
return map;
}
示例4: jsonString2Bean
import net.sf.json.JSONException; //導入依賴的package包/類
/**
*
* @param jsonStr
* 需轉換的標準的json字符串
* @param clz
* 轉換對象
* @param classMap
* 轉換對象中包含複雜對象,key對應jsonStr中key,value對應需轉換為的複雜對象
* @return
*/
public static <T> T jsonString2Bean(String jsonStr, Class<T> clz, Map<String, Class<?>> classMap) {
JSONObject json_obj = null;
try {
json_obj = JSONObject.fromObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
}
if (json_obj == null)
return null;
JsonConfig config = new JsonConfig();
config.setRootClass(clz);
config.setClassMap(classMap);
config.setHandleJettisonEmptyElement(true);
return (T) JSONObject.toBean(json_obj, config);
}
示例5: validate
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* Returns <code>true</code> if the file has matching security details. The credentials are
* compared with the currently logged in user's credentials.
*
* @param fileAsJson The file under concern with extension from setting.xml
* @return <code>true</code> if the file has matching security details.
*/
@Override
public boolean validate(JSONObject fileAsJson) {
try {
if (!"true".equalsIgnoreCase(fileAsJson.getString("visible"))) {
return false;
}
Object unDeterminedObject = fileAsJson.get("security");
if (unDeterminedObject instanceof JSONObject) {
return RulesUtils.validateUser(AuthenticationUtils.getUserDetails(), (JSONObject) unDeterminedObject);
} else {
String security = fileAsJson.getString("security");
return RulesUtils.validateUser(AuthenticationUtils.getUserDetails(), security);
}
} catch (JSONException ex) {
logger.error("The resource is malformed. Setting.xml is configured to apply rules. " +
"But the resource does not support " + "the rules.", ex);
}
return false;
}
示例6: prepareExtensionsMap
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* Prepares the member variable visibleExtensionsMap
*
* @param visibleExtensions The JSON from the setting.xml for which the visible attribute
* is provided to be true
*/
private void prepareExtensionsMap(JSONObject visibleExtensions) {
Assert.notNull(visibleExtensions, "Visible extension's JSON is null!");
Iterator<?> iterator = visibleExtensions.keys();
this.visibleExtensionsMap = new HashMap<>();
while (iterator.hasNext()) {
int counter = 0;
String key = (String) iterator.next();
try {
iterate(visibleExtensions, counter, key);
} catch (JSONException ex) {
// If the key is not a JSON object move to the next key
}
}
if (logger.isDebugEnabled()) {
logger.debug("List of visible extensions that will be viewable " +
"in the repository is " +
visibleExtensionsMap);
}
}
示例7: prepareFoldersTagMap
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* Prepares the member variables foldersTagMap and generic rules related
* member variables
*
* @param visibleExtensions The JSON from the setting.xml for which the visible attribute
* is provided to be true
*/
private void prepareFoldersTagMap(JSONObject visibleExtensions) {
Assert.notNull(visibleExtensions, "Visible extension's JSON is null!");
try {
JSONObject foldersJSONObject = initialize(visibleExtensions);
/*
* genericRulesJSON has been prepared. Now prepare the foldersTagMap
*/
prepareTextAndRuleValuesList(foldersJSONObject);
} catch (JSONException ignore) {
}
if (logger.isDebugEnabled()) {
logger.debug("The configured folder rule setting is " + foldersTagMap);
}
}
示例8: initialize
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* Sets the booleans related to generic rules, prepares the genericRulesArray or
* genericRulesJSON accordingly.
* Returns the foldersTagMap.
*
* @param visibleExtensions The JSON from the setting.xml for which the visible attribute is
* provided to be true
* @return JSONObject of folder tag in setting.xml
*/
private JSONObject initialize(JSONObject visibleExtensions) {
JSONObject foldersJSONObject = visibleExtensions.getJSONObject("folder");
this.foldersTagMap = new HashMap<>();
useFolderRules = true;
try {
genericRulesJSONArray = foldersJSONObject.getJSONArray("generic");
isGenericRulePresent = true;
multipleGenericRules = true;
} catch (JSONException ex) {
try {
genericRulesJSON = new JSONObject();
genericRulesJSON.accumulate("generic", foldersJSONObject.getString("generic"));
} catch (JSONException ignore) {
}
}
return foldersJSONObject;
}
示例9: getRules
import net.sf.json.JSONException; //導入依賴的package包/類
private static List<String> getRules(List<String> rules, JSONObject rulesJson) {
try {
rules.add(0, rulesJson.getJSONObject("rule").getString("@class"));
} catch (JSONException ex) {
try {
JSONArray rulesArray = rulesJson.getJSONArray("rule");
Iterator<?> iterator = rulesArray.iterator();
//noinspection WhileLoopReplaceableByForEach
while (iterator.hasNext()) {
rules.add(((JSONObject) iterator.next()).getString("@class"));
}
} catch (Exception e) {
throw new XmlConfigurationException("The configuration of setting.xml is " +
"incorrect. Expected class attribute(s) for the rules node child node(s) " +
"of security tag.", e);
}
}
return rules;
}
示例10: getEfwResultExtension
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* This method is used to get the extension for the file which need to be
* create
*
* @return String
*/
public static String getEfwResultExtension() {
JSONObject settingsJson = getSettingsJson();
String extension = null;
try {
JSONObject extensionsJSONObject = settingsJson.getJSONObject("Extentions").getJSONObject("efwresult");
if (extensionsJSONObject == null) {
throw new ApplicationException();
}
extension = extensionsJSONObject.getString("#text");
} catch (JSONException ex) {
logger.error("Please provide attribute visible to be true in the Extensions tag for " + "efwresult", ex);
} catch (ApplicationException e) {
logger.error("The object extensionsJSONObject is null.");
}
return extension;
}
示例11: getEFWSRExtension
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* <p>
* Obtains the efwsr file extension from the setting.xml file
* </p>
*
* @return A string that is the key value of efwsr in setting.xml
*/
public static String getEFWSRExtension() {
ApplicationProperties properties = ApplicationProperties.getInstance();
IProcessor processor = ResourceProcessorFactory.getIProcessor();
JSONObject json = processor.getJSONObject(properties.getSettingPath(), false);
String extension = null;
try {
JSONObject extensionsJSONObject = json.getJSONObject("Extentions").getJSONObject("efwsr");
if (extensionsJSONObject == null) {
throw new ApplicationException();
}
extension = extensionsJSONObject.getString("#text");
} catch (JSONException ex) {
logger.error("Please provide attribute visible to be true in the Extensions tag for " + "efwsr", ex);
} catch (ApplicationException e) {
logger.error("The object extensionsJSONObject is null.");
}
return extension;
}
示例12: getFolderFileExtension
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* <p>
* Gets efwFolder key value from setting.xml. If it is not present in
* setting.xml, it will throw exception else return value of efwFolder node.
* </p>
*
* @return a <code>String</code> which specifies value of efwFolder node in
* setting.xml
*/
public static String getFolderFileExtension() {
ApplicationProperties applicationProperties = ApplicationProperties.getInstance();
IProcessor processor = ResourceProcessorFactory.getIProcessor();
JSONObject json = processor.getJSONObject(applicationProperties.getSettingPath(), false);
String extension = null;
try {
JSONObject extensionsJSONObject = json.getJSONObject("Extentions").getJSONObject("folder").getJSONObject
("efwFolder");
if (extensionsJSONObject == null) {
throw new ImproperXMLConfigurationException("Setting.xml configuration is " + "incorrect");
}
extension = extensionsJSONObject.getString("#text");
} catch (JSONException ex) {
logger.error("Exception while retrieving efwFolder key's value", ex);
} catch (ImproperXMLConfigurationException e) {
logger.error("ImproperXMLConfigurationException", e);
}
return extension;
}
示例13: replaceParameters
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* <p>
* The parameters in the templateData will be replaced with the
* corresponding values from the parametersJSON
* </p>
*
* @param templateData The content of the template file as string
* @param parametersJSON The json of the parameters
* @return Updated template html file as string
*/
String replaceParameters(String templateData, JSONObject parametersJSON) {
Iterator<?> keys = parametersJSON.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (templateData.contains("${" + key + "}")) {
try {
JSONArray array = parametersJSON.getJSONArray(key);
String value = array.toString();
logger.debug("value = " + value);
value = value.replace("[", "").replace("]", "").replace("\"", "'");
logger.debug("The value for key " + key + "is " + value);
templateData = templateData.replace("${" + key + "}", value);
} catch (JSONException ex) {
logger.debug("key " + key + "is not an array" + ". Key value = " +
parametersJSON.getString(key));
templateData = templateData.replace("${" + key + "}", parametersJSON.getString(key));
}
}
}
return templateData;
}
示例14: getJSONArray
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* <p>
* This method creates net.sf.Json.JSONArray using List of Maps
* </p>
*
* @param listOfMaps a list of map objects
* @return a <code>String</code> which is a JSONArray.
*/
public static JSONArray getJSONArray(List<Map<String, String>> listOfMaps) {
JSONArray jsonArray = new JSONArray();
for (Map<String, String> map : listOfMaps) {
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
try {
jsonObject.put(key, value);
} catch (JSONException e) {
logger.error("JSONException", e);
}
}
jsonArray.add(jsonObject);
}
return jsonArray;
}
示例15: deleteFavouriteFile
import net.sf.json.JSONException; //導入依賴的package包/類
/**
* Deletes the specified favourite file
*
* @param reportFile The name of the file
* @param reportDirectory The location of reportFile
*/
public void deleteFavouriteFile(String reportFile, String reportDirectory) {
File xmlFile = new File(this.solutionDirectory + File.separator + reportDirectory + File.separator +
reportFile);
IProcessor processor = ResourceProcessorFactory.getIProcessor();
JSONObject jsonObject = processor.getJSONObject(xmlFile.toString(), false);
try {
String fileTobeSearched = jsonObject.getString("favourite");
logger.info("Trying to delete the file " + fileTobeSearched);
boolean result = deleteFile(this.solutionDirectory, fileTobeSearched);
logger.debug("File " + fileTobeSearched + " deleted status " + result);
} catch (JSONException ex) {
logger.error("JSONException", ex);
}
}