本文整理了Java中org.codehaus.jettison.json.JSONObject.put()
方法的一些代码示例,展示了JSONObject.put()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.put()
方法的具体详情如下:
包路径:org.codehaus.jettison.json.JSONObject
类名称:JSONObject
方法名:put
[英]Put a key/double pair in the JSONObject.
[中]在JSONObject中放置一个密钥/双精度对。
代码示例来源:origin: Netflix/Priam
private JSONObject createJson(
String primaryEndpoint,
String dataCenter,
String rack,
String status,
String state,
String load,
String owns,
String token)
throws JSONException {
JSONObject object = new JSONObject();
object.put("endpoint", primaryEndpoint);
object.put("dc", dataCenter);
object.put("rack", rack);
object.put("status", status);
object.put("state", state);
object.put("load", load);
object.put("owns", owns);
object.put("token", token);
return object;
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected void addCategoryProductData(final HttpServletRequest request, final JSONObject categoryData) throws JSONException {
final List<Product> products = getProducts(request);
final JSONArray itemList = new JSONArray();
for (int i = 0; i < products.size(); i++) {
JSONObject item = new JSONObject();
item.put("@type", "ListItem");
item.put("position", i + 1);
item.put("url", products.get(i).getUrl());
itemList.put(item);
extensionManager.getProxy().addCategoryProductData(request, categoryData);
}
categoryData.put("itemListElement", itemList);
}
代码示例来源:origin: org.codehaus.jettison/jettison
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONArray which is produced from a Collection.
* @param key A key string.
* @param value A Collection value.
* @return this.
* @throws JSONException JSONException
*/
public JSONObject put(String key, Collection value) throws JSONException {
put(key, new JSONArray(value));
return this;
}
代码示例来源:origin: Netflix/Priam
@GET
@Path("/decompress")
public Response decompress(@QueryParam("in") String in, @QueryParam("out") String out)
throws Exception {
SnappyCompression compress = new SnappyCompression();
compress.decompressAndClose(new FileInputStream(in), new FileOutputStream(out));
JSONObject object = new JSONObject();
object.put("Input compressed file", in);
object.put("Output decompress file", out);
return Response.ok(object.toString(), MediaType.APPLICATION_JSON).build();
}
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected JSONArray addContactData(final HttpServletRequest request) throws JSONException {
final JSONArray contactList = new JSONArray();
final JSONObject contact = new JSONObject();
contact.put("@type", "ContactPoint");
contact.put("telephone", getSiteCustomerServiceNumber());
contact.put("contactType", "customerService");
extensionManager.getProxy().addContactData(request, contact);
contactList.put(contact);
return contactList;
}
代码示例来源:origin: org.codehaus.jettison/jettison
public void writeCharacters(String text) throws XMLStreamException {
text = text.trim();
if (text.length() == 0) {
return;
}
try {
Object o = getCurrentNode().opt("$");
if (o instanceof JSONArray) {
((JSONArray) o).put(text);
} else if (o instanceof String) {
JSONArray arr = new JSONArray();
arr.put(o);
arr.put(text);
getCurrentNode().put("$", arr);
} else {
getCurrentNode().put("$", text);
}
} catch (JSONException e) {
throw new XMLStreamException(e);
}
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
/**
* Generates an object representing the Schema.org WebSite
*
* @return JSON representation of WebSite from Schema.org
*/
protected JSONObject addWebSiteData(final HttpServletRequest request) throws JSONException {
JSONObject webSite = new JSONObject();
webSite.put("@context", DEFAULT_STRUCTURED_CONTENT_CONTEXT);
webSite.put("@type", "WebSite");
webSite.put("name", getSiteName());
webSite.put("url", getSiteBaseUrl());
webSite.put("potentialAction", addPotentialActions(request));
extensionManager.getProxy().addWebSiteData(request, webSite);
return webSite;
}
代码示例来源:origin: Netflix/Priam
return Response.status(503).entity("JMXConnectionException").build();
JSONObject rootObj = new JSONObject();
CompactionManagerMBean cm = nodeTool.getCompactionManagerProxy();
rootObj.put("pending tasks", cm.getPendingTasks());
JSONArray compStats = new JSONArray();
for (Map<String, String> c : cm.getCompactions()) {
JSONObject cObj = new JSONObject();
cObj.put("id", c.get("id"));
cObj.put("keyspace", c.get("keyspace"));
cObj.put("columnfamily", c.get("columnfamily"));
cObj.put("bytesComplete", c.get("bytesComplete"));
cObj.put("totalBytes", c.get("totalBytes"));
cObj.put("taskType", c.get("taskType"));
String percentComplete =
new Long(c.get("totalBytes")) == 0
* 100)
+ "%";
cObj.put("progress", percentComplete);
compStats.put(cObj);
rootObj.put("compaction stats", compStats);
return Response.ok(rootObj, MediaType.APPLICATION_JSON).build();
代码示例来源:origin: org.codehaus.jettison/jettison
/**
* Accumulate values under a key. It is similar to the put method except
* that if there is already an object stored under the key then a
* JSONArray is stored under the key to hold all of the accumulated values.
* If there is already a JSONArray, then the new value is appended to it.
* In contrast, the put method replaces the previous value.
* @param key A key string.
* @param value An object to be accumulated under the key.
* @return this.
* @throws JSONException If the value is an invalid number
* or if the key is null.
*/
public JSONObject accumulate(String key, Object value)
throws JSONException {
testValidity(value);
Object o = opt(key);
if (o == null) {
put(key, value);
} else if (o instanceof JSONArray) {
((JSONArray)o).put(value);
} else {
put(key, new JSONArray().put(o).put(value));
}
return this;
}
代码示例来源:origin: Netflix/Priam
@GET
@Path("/compact")
public Response cassCompact() throws IOException, ExecutionException, InterruptedException {
JSONObject rootObj = new JSONObject();
try {
compaction.execute();
rootObj.put("Compcated", true);
return Response.ok().entity(rootObj).build();
} catch (Exception e) {
try {
rootObj.put("status", "ERRROR");
rootObj.put("desc", e.getLocalizedMessage());
} catch (Exception e1) {
return Response.status(503).entity("CompactionError").build();
}
return Response.status(503).entity(rootObj).build();
}
}
代码示例来源:origin: Netflix/Priam
JSONArray threadPoolArray = new JSONArray();
while (threads.hasNext()) {
Entry<String, JMXEnabledThreadPoolExecutorMBean> thread = threads.next();
JMXEnabledThreadPoolExecutorMBean threadPoolProxy = thread.getValue();
JSONObject tpObj = new JSONObject(); // "Pool Name", "Active",
tpObj.put("pool name", thread.getKey());
tpObj.put("active", threadPoolProxy.getActiveCount());
tpObj.put("pending", threadPoolProxy.getPendingTasks());
tpObj.put("completed", threadPoolProxy.getCompletedTasks());
tpObj.put("blocked", threadPoolProxy.getCurrentlyBlockedTasks());
tpObj.put("total blocked", threadPoolProxy.getTotalBlockedTasks());
threadPoolArray.put(tpObj);
JSONObject droppedMsgs = new JSONObject();
for (Entry<String, Integer> entry : nodeTool.getDroppedMessages().entrySet())
droppedMsgs.put(entry.getKey(), entry.getValue());
JSONObject rootObj = new JSONObject();
rootObj.put("thread pool", threadPoolArray);
rootObj.put("dropped messages", droppedMsgs);
代码示例来源:origin: org.codehaus.jettison/jettison
values = (JSONArray)old;
} else {
values = new JSONArray();
values.put(old);
object.put(property.getKey(), values);
} else if(getSerializedAsArrays().contains(getPropertyArrayKey(property))) {
JSONArray values = new JSONArray();
boolean emptyString = value instanceof String && ((String)value).isEmpty();
if (!convention.isIgnoreEmptyArrayValues()
values.put(value);
object.put(property.getKey(), values);
} else {
object.put(property.getKey(), value);
代码示例来源:origin: Netflix/Priam
@GET
@Path("/flush")
public Response cassFlush() throws IOException, InterruptedException, ExecutionException {
JSONObject rootObj = new JSONObject();
try {
flush.execute();
rootObj.put("Flushed", true);
return Response.ok().entity(rootObj).build();
} catch (Exception e) {
try {
rootObj.put("status", "ERRROR");
rootObj.put("desc", e.getLocalizedMessage());
} catch (Exception e1) {
return Response.status(503).entity("FlushError").build();
}
return Response.status(503).entity(rootObj).build();
}
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected void addReviewData(final HttpServletRequest request, final Product product, final JSONObject productData) throws JSONException {
final RatingSummary ratingSummary = ratingService.readRatingSummary(product.getId().toString(), RatingType.PRODUCT);
if (ratingSummary != null && ratingSummary.getNumberOfRatings() > 0) {
final JSONObject aggregateRating = new JSONObject();
aggregateRating.put("ratingCount", ratingSummary.getNumberOfRatings());
aggregateRating.put("ratingValue", ratingSummary.getAverageRating());
extensionManager.getProxy().addAggregateReviewData(request, product, aggregateRating);
productData.put("aggregateRating", aggregateRating);
final JSONArray reviews = new JSONArray();
for (final ReviewDetail reviewDetail : ratingSummary.getReviews()) {
final JSONObject review = new JSONObject();
review.put("reviewBody", reviewDetail.getReviewText());
review.put("reviewRating", new JSONObject().put("ratingValue", reviewDetail.getRatingDetail().getRating()));
review.put("author", reviewDetail.getCustomer().getFirstName());
review.put("datePublished", ISO_8601_FORMAT.format(reviewDetail.getReviewSubmittedDate()));
extensionManager.getProxy().addReviewData(request, product, review);
reviews.put(review);
}
productData.put("review", reviews);
}
}
}
代码示例来源:origin: org.codehaus.jettison/jettison
/**
* Append values to the array under a key. If the key does not exist in the
* JSONObject, then the key is put in the JSONObject with its value being a
* JSONArray containing the value parameter. If the key was already
* associated with a JSONArray, then the value parameter is appended to it.
* @param key A key string.
* @param value An object to be accumulated under the key.
* @return this.
* @throws JSONException If the key is null or if the current value
* associated with the key is not a JSONArray.
*/
public JSONObject append(String key, Object value)
throws JSONException {
testValidity(value);
Object o = opt(key);
if (o == null) {
put(key, new JSONArray().put(value));
} else if (!(o instanceof JSONArray)){
throw new JSONException("JSONObject[" + key +
"] is not a JSONArray.");
} else {
((JSONArray)o).put(value);
}
return this;
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected JSONObject addPotentialActions(final HttpServletRequest request) throws JSONException {
final JSONObject potentialAction = new JSONObject();
potentialAction.put("@type", "SearchAction");
potentialAction.put("target", getSiteBaseUrl() + getSiteSearchUri());
potentialAction.put("query-input", "required name=query");
extensionManager.getProxy().addPotentialActionsData(request, potentialAction);
return potentialAction;
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected void addSkus(final HttpServletRequest request, final Product product, final JSONObject productData, final String url)
throws JSONException {
final JSONArray offers = new JSONArray();
final String currency = product.getRetailPrice().getCurrency().getCurrencyCode();
BigDecimal highPrice = BigDecimal.ZERO;
final JSONObject offer = new JSONObject();
offer.put("@type", "Offer");
offer.put("sku", sku.getId());
offer.put("priceCurrency", currency);
offer.put("availability", determineAvailability(sku));
offer.put("url", url);
offer.put("category", product.getCategory().getName());
offer.put("priceValidUntil", ISO_8601_FORMAT.format(sku.getActiveEndDate()));
offer.put("price", price.getAmount());
final JSONObject aggregateOffer = new JSONObject();
aggregateOffer.put("@type", "AggregateOffer");
aggregateOffer.put("highPrice", highPrice.doubleValue());
aggregateOffer.put("lowPrice", lowPrice.doubleValue());
aggregateOffer.put("priceCurrency", currency);
aggregateOffer.put("offerCount", offers.length());
aggregateOffer.put("offers", offers);
productData.put("offers", aggregateOffer);
} else {
productData.put("offers", offers);
代码示例来源:origin: org.codehaus.jettison/com.springsource.org.codehaus.jettison
/**
* Put a key/value pair in the JSONObject, where the value will be a
* JSONArray which is produced from a Collection.
* @param key A key string.
* @param value A Collection value.
* @return this.
* @throws JSONException
*/
public JSONObject put(String key, Collection value) throws JSONException {
put(key, new JSONArray(value));
return this;
}
代码示例来源:origin: Netflix/Priam
private static JSONObject parseGossipInfo(String gossipinfo) throws JSONException {
String[] ginfo = gossipinfo.split("\n");
JSONObject rootObj = new JSONObject();
JSONObject obj = new JSONObject();
String key = "";
for (String line : ginfo) {
if (line.matches("^.*/.*$")) {
String[] data = line.split("/");
if (StringUtils.isNotBlank(key)) {
rootObj.put(key, obj);
obj = new JSONObject();
}
key = data[1];
} else if (line.matches("^ .*:.*$")) {
String[] kv = line.split(":");
kv[0] = kv[0].trim();
if (kv[0].equals("STATUS")) {
obj.put(kv[0], kv[1]);
String[] vv = kv[1].split(",");
obj.put("Token", vv[1]);
} else {
obj.put(kv[0], kv[1]);
}
}
}
if (StringUtils.isNotBlank(key)) rootObj.put(key, obj);
return rootObj;
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
final JSONObject breadcrumbObjects = new JSONObject();
breadcrumbObjects.put("@context", getStructuredDataContext());
breadcrumbObjects.put("@type", "BreadcrumbList");
final JSONArray breadcrumbList = new JSONArray();
int index = 1;
final JSONObject listItem = new JSONObject();
listItem.put("@type", "ListItem");
listItem.put("position", index);
final JSONObject item = new JSONObject();
item.put("@id", getSiteBaseUrl() + breadcrumb.getLink());
item.put("name", breadcrumb.getText());
listItem.put("item", item);
breadcrumbObjects.put("itemListElement", breadcrumbList);
内容来源于网络,如有侵权,请联系作者删除!