本文整理汇总了Java中us.monoid.json.JSONObject类的典型用法代码示例。如果您正苦于以下问题:Java JSONObject类的具体用法?Java JSONObject怎么用?Java JSONObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JSONObject类属于us.monoid.json包,在下文中一共展示了JSONObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDevices
import us.monoid.json.JSONObject; //导入依赖的package包/类
public List<HomeWizardSmartPlugDevice> getDevices()
{
List<HomeWizardSmartPlugDevice> homewizardDevices = new ArrayList<>();
try {
String result = requestJson(EMPTY_STRING);
JSONObject resultJson = new JSONObject(result);
cloudPlugId = resultJson.getString("id");
String all_devices_json = resultJson.get("devices").toString();
Device[] devices = gson.fromJson(all_devices_json, Device[].class);
// Fix names from JSON
for (Device device : devices) {
device.setTypeName(StringUtils.capitalize(device.getTypeName().replace("_", " ")));
homewizardDevices.add(mapDeviceToHomeWizardSmartPlugDevice(device));
}
}
catch(JSONException e) {
log.warn("Error while get devices from cloud service ", e);
}
log.info("Found: " + homewizardDevices.size() + " devices");
return homewizardDevices;
}
示例2: execApply
import us.monoid.json.JSONObject; //导入依赖的package包/类
public void execApply(String jsonToPost) throws JSONException, IOException {
// Extract
JSONObject resultJson = new JSONObject(jsonToPost);
String deviceId = resultJson.getString("deviceid");
String action = resultJson.getString("action");
// Check if we have an plug id stored
if (StringUtils.isBlank(cloudPlugId)) {
getDevices();
}
// Send request to HomeWizard cloud
if (!sendAction("/" + cloudPlugId + "/devices/" + deviceId + "/action", action))
{
throw new IOException("Send action to HomeWizard Cloud failed.");
}
}
示例3: getJSON
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
* @param url
* @return Returns the JSON
*/
private String getJSON(String url){
JSONResource res = null;
JSONObject obj = null;
Resty resty = new Resty();
try {
res = resty.json(url);
obj = res.toObject();
} catch (IOException | JSONException e) {
// TODO: not my favorite way but does the trick
// e.printStackTrace();
return getJSON(url);
}
return obj.toString();
}
示例4: getDataFromDimension
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
* @param jsonObject
* @return Return a List with all the data from JSON
*/
private List<String> getDataFromDimension(String jsonObject) {
List<String> data = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonObject);
us.monoid.json.JSONArray jsonArray = jobj.getJSONArray("cells");
for (int i = 0; i < jsonArray.length() ; i++) {
JSONObject row = jsonArray.getJSONObject(i);
data.add(row.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
return data;
}
示例5: nextPage
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
* Attempt to request the next page using the next link from the current page
* @return a BundleSearchResults representing the next page, or null if there are no other pages
* @throws JSONException if the href for the next link could not be obtained for some reason
* @throws IOException if a network failure occurred while fetching the next page
*/
public BundleSearchResults nextPage() throws JSONException, IOException {
if(!hasNextPage()) {
return null;
}
JSONObject nextLink = nextLink();
if(nextLink != null) {
String href = (String)nextLink.get("href");
JSONResource jsonResource =
client.json(client.buildPathFromHref(href));
ClarifyResponse resp = new ClarifyResponse(jsonResource);
BundleSearchResults results = new BundleSearchResults(client, resp);
return results;
}
return null;
}
示例6: nextPage
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
* Attempt to request the next page using the next link from the current page
* @return a BundleList representing the next page, or null if there are no other pages
* @throws JSONException if the href for the next link could not be obtained for some reason
* @throws IOException if a network failure occurred while fetching the next page
*/
public BundleList nextPage() throws JSONException, IOException {
if(!hasNextPage()) {
return null;
}
JSONObject nextLink = nextLink();
if(nextLink != null) {
String href = (String)nextLink.get("href");
JSONResource jsonResource =
client.json(client.buildPathFromHref(href));
ClarifyResponse resp = new ClarifyResponse(jsonResource);
BundleList list = new BundleList(client, resp);
return list;
}
return null;
}
示例7: updateMetadata
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
* Updates the user-defined data property of the Bundle's Metadata with the supplied JSON string,
* then return a refreshed copy (resulting in 2 API calls)
* @param bundleId the GUID of the Bundle for updating the Metadata
* @param json a String containing valid JSON, or null. If null is passed, then the data is reset to a JSON equiv of {}
* @return a refreshed Metadata instance for the media bundle
* @throws IOException if a failure occurred during the API,
* typically a 4xx HTTP error code + JSON payload with the error message and details
*/
public BundleMetadata updateMetadata(String bundleId, String json) throws IOException {
if(bundleId == null) {
throw new RuntimeException("bundleId cannot be null");
}
if(json == null) {
json = "{}";
}
// wrap the request in a JSON payload with a data property that contains the user JSON data to update
JSONObject payload = null;
try {
payload = new JSONObject().put("data", json);
} catch (JSONException e) {
throw new RuntimeException(e);
}
JSONResource jsonResource =
json(buildPathFromResourcePath("/bundles/"+bundleId+"/metadata"), put(content(payload)));
// re-retrieve
return findMetadata(bundleId);
}
示例8: execute
import us.monoid.json.JSONObject; //导入依赖的package包/类
@Override
public HttpResponse execute(final String url, final String jsonRequestBody) {
HttpURLConnection httpConnection = null;
try {
final Resty resty = new Resty();
if (isProxySet()) {
resty.setProxy(proxyHost, proxyPort);
}
final TextResource textResource = resty.text(url, content(new JSONObject(jsonRequestBody)));
httpConnection = textResource.http();
return toHttpResponse(textResource, httpConnection);
} catch (IOException ioEx) {
throw new HipChatNotificationPluginException("Error opening connection to HipChat URL: [" + ioEx.getMessage() + "].", ioEx);
} catch (JSONException jsonEx) {
throw new HipChatNotificationPluginException("Malformed JSON request body: [" + jsonEx.getMessage() + "].", jsonEx);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
示例9: accquireToken
import us.monoid.json.JSONObject; //导入依赖的package包/类
private String accquireToken() throws RestClientException {
String securityToken = null;
if (!isServiceRunning()) {
throw new RestClientException("Id service is not currently running at URL:" + idServiceUrl);
}
LOGGER.info("Id service rest client logs in to get a new security token." );
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", this.userName);
jsonObject.put("password", this.password);
securityToken = (String) resty.json(urlHelper.getLoginUrl(), RestyHelper.content(jsonObject)).get(TOKEN);
LOGGER.info("Security token is acquired successfully:" + securityToken );
} catch (Exception e) {
throw new RestClientException("Failed to login for user name:" + this.userName, e);
}
return securityToken;
}
示例10: isTokenValid
import us.monoid.json.JSONObject; //导入依赖的package包/类
private boolean isTokenValid(String token) {
if (token == null) {
return false;
}
boolean isValid = false;
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(TOKEN, token);
JSONResource response = resty.json(urlHelper.getTokenAuthenticationUrl(), RestyHelper.content(jsonObject,APPLICATION_JSON));
if (response != null) {
if (HttpStatus.SC_OK == (response.getHTTPStatus())) {
isValid = true;
} else {
LOGGER.info("Inavlid token with failure reason from id server:" + response.get(MESSAGE));
}
}
} catch (Exception e) {
LOGGER.error("Failed to valid token:" + token, e);
}
return isValid;
}
示例11: logOut
import us.monoid.json.JSONObject; //导入依赖的package包/类
@Override
public void logOut() throws RestClientException {
currentSessions.getAndDecrement();
synchronized (LOCK) {
if (token != null) {
LOGGER.info("Total current sessions:" + currentSessions.get());
if (currentSessions.get() == 0) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put(TOKEN, token);
resty.json(urlHelper.getLogoutUrl(), RestyHelper.content((jsonObject)));
LOGGER.info("Id service rest client logs out successfully with token:" + token );
token = null;
} catch (Exception e) {
throw new RestClientException("Failed to login out " + this.userName, e);
}
}
}
}
}
示例12: login
import us.monoid.json.JSONObject; //导入依赖的package包/类
public boolean login()
{
try
{
URL url = new URL(HOMEWIZARD_LOGIN_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", cloudAuth);
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder buffer = new StringBuilder();
String line;
while((line = br.readLine()) != null)
{
buffer.append(line).append("\n");
}
br.close();
// Get session id from result JSON
JSONObject json = new JSONObject(buffer.toString());
cloudSessionId = json.get("session").toString();
}
catch(IOException | JSONException e)
{
log.warn("Error while login to cloud service ", e);
return false;
}
return true;
}
示例13: loadFromJSON
import us.monoid.json.JSONObject; //导入依赖的package包/类
public static OnlineGame loadFromJSON(JSONObject object) throws JSONException {
return new OnlineGame(
object.getInt("id"),
RageGoServer.getPlayer(object.getInt("whites_id")),
RageGoServer.getPlayer(object.getInt("blacks_id"))
);
}
示例14: nextLink
import us.monoid.json.JSONObject; //导入依赖的package包/类
/**
*
* @return the JSONObject for the next link under the _links, or null if not found
*/
protected JSONObject nextLink() {
try {
JSONObject links = (JSONObject)response.getJSONValue("_links");
return (JSONObject)links.get("next");
} catch (JSONException e) {
// a JSONException is thrown if not found
}
return null;
}
示例15: waitForTaskToFinish
import us.monoid.json.JSONObject; //导入依赖的package包/类
public JSONObject waitForTaskToFinish(String node, String taskId) throws IOException, LoginException, JSONException {
JSONObject lastTaskStatus = null;
Boolean isRunning = true;
while (isRunning) {
lastTaskStatus = getTaskStatus(node, taskId);
isRunning = (lastTaskStatus.getString("status").equals("running"));
}
return lastTaskStatus;
}