本文整理汇总了Java中com.ibm.json.java.JSONArray类的典型用法代码示例。如果您正苦于以下问题:Java JSONArray类的具体用法?Java JSONArray怎么用?Java JSONArray使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JSONArray类属于com.ibm.json.java包,在下文中一共展示了JSONArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getComments
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private JSONArray getComments(String itemNumber) {
JSONArray returnArray = new JSONArray();
CouchDbConnector dbc = _db.createConnector(dbname, true);
Map<String, String> doc = new HashMap<String, String>();
ViewQuery query = new ViewQuery().allDocs().includeDocs(true);
List<Map> result = dbc.queryView(query, Map.class);
JSONArray jsonresult = new JSONArray();
for (Map element : result) {
JSONObject obj = new JSONObject();
obj.putAll(element);
if(itemNumber==null || obj.get("itemNumber").equals(itemNumber))
jsonresult.add(obj);
}
System.out.println(jsonresult.toString());
return jsonresult;
}
示例2: PIBeacon
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
public PIBeacon(JSONObject beaconObj) {
JSONObject geometry = (JSONObject)beaconObj.get("geometry");
JSONObject properties = (JSONObject)beaconObj.get("properties");
code = (String) properties.get(JSON_CODE);
name = (String) properties.get(JSON_NAME);
description = properties.get(JSON_DESCRIPTION) != null ? (String)properties.get(JSON_DESCRIPTION) : "";
proximityUUID = (String) properties.get(JSON_PROXIMITY_UUID);
major = (String) properties.get(JSON_MAJOR);
minor = (String) properties.get(JSON_MINOR);
threshold = objToDouble(properties.get(JSON_THRESHOLD));
JSONArray coordinates = (JSONArray) geometry.get("coordinates");
x = objToDouble(coordinates.get(0));
y = objToDouble(coordinates.get(1));
}
示例3: formatTree
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private static void formatTree(JSONObject node, int level, List<Map<String,String>> arr) {
if (node == null) return;
JSONArray children = (JSONArray)node.get("children");
if (level > 0 && (children == null || level != 2)) {
Map<String,String> obj = new HashMap<String,String>();
obj.put("id", (String)node.get("id"));
if (children != null) obj.put("title", "true");
if (node.containsKey("percentage")) {
double p = (Double)node.get("percentage");
p = Math.floor(p * 100.0);
obj.put("value", Double.toString(p) + "%");
}
arr.add(obj);
}
if (children != null && !"sbh".equals(node.get("id"))) {
for (int i = 0; i < children.size(); i++) {
formatTree((JSONObject)children.get(i), level + 1, arr);
}
}
}
示例4: buildContent
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private JSONObject buildContent(String text)
{
JSONObject contentItem = new JSONObject();
contentItem.put("userid", UUID.randomUUID().toString());
contentItem.put("id", UUID.randomUUID().toString());
contentItem.put("sourceid", "freetext");
contentItem.put("contenttype", "text/plain");
contentItem.put("language", "en");
contentItem.put("content", text);
JSONObject content = new JSONObject();
JSONArray contentItems = new JSONArray();
content.put("contentItems", contentItems);
contentItems.add(contentItem);
return content;
}
示例5: readHostnameFromVcapEnvironment
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private static String readHostnameFromVcapEnvironment() {
String vcapApplication = System.getenv().get("VCAP_APPLICATION");
if (vcapApplication != null) {
try {
JSONObject p = (JSONObject) JSON.parse(vcapApplication);
JSONArray uris = (JSONArray) p.get("application_uris");
// Take the first uri
if (uris != null) {
return (String) uris.iterator().next();
}
} catch (IOException e) {
// Let's log and ignore this case and drop through to the
// default case
e.printStackTrace();
}
}
return null;
}
示例6: doGet
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
JSONArray result = new JSONArray();
switch (action) {
case "getComments":
result = getComments(request.getParameter("itemNumber"));
break;
case "addComment":
addComment(request.getParameter("itemNumber"), request.getParameter("comment"));
break;
}
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getOutputStream().print(result.serialize());
return;
}
示例7: getDeviceByDescriptor
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
/**
* Retrieves a device within an organization by its descriptor.
*
* @param deviceDescriptor unique identifier for the device.
* @param completionHandler callback for APIs asynchronous calls. Result returns as {@link PIDevice PIDevice}.
*
* @deprecated use {@link #getDevice(String, PIAPICompletionHandler)}. When you register a device,
* the method will return a PIDevice object that contains the device documents code.
*/
@Deprecated
public void getDeviceByDescriptor(String deviceDescriptor, final PIAPICompletionHandler completionHandler) {
String device = String.format("%s/tenants/%s/orgs/%s/devices?rawDescriptor=%s", mServerURL, mTenantCode, mOrgCode, deviceDescriptor);
try {
URL url = new URL(device);
GET(url, new PIAPICompletionHandler() {
@Override
public void onComplete(PIAPIResult result) {
if (result.getResponseCode() == 200) {
JSONArray matchingDevices = (JSONArray)result.getResultAsJson().get(JSON_ROWS);
if (matchingDevices.size() > 0) {
result.setResult(new PIDevice((JSONObject) matchingDevices.get(0)));
}
}
completionHandler.onComplete(result);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
示例8: getPolygonsFromJson
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private ArrayList<ArrayList<Point>> getPolygonsFromJson(JSONArray coordinates) {
ArrayList<ArrayList<Point>> polygons = new ArrayList<ArrayList<Point>>();
for (int i = 0; i < coordinates.size(); i++) {
JSONArray polygon = (JSONArray) coordinates.get(i);
ArrayList<Point> points = new ArrayList<Point>();
for (int j = 0; j < polygon.size(); j++) {
JSONArray point = (JSONArray)polygon.get(j);
points.add(new Point(objToInt(point.get(0)), objToInt(point.get(0))));
}
polygons.add(points);
}
return polygons;
}
示例9: buildBeaconPayload
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
private JSONObject buildBeaconPayload(Collection<Beacon> beacons) {
long detectedTime = System.currentTimeMillis();
JSONObject payload = new JSONObject();
JSONArray beaconArray = new JSONArray();
// build payload with nearest beacon only
Beacon nearestBeacon = beacons.iterator().next();
for (Beacon b : beacons) {
if (b.getDistance() < nearestBeacon.getDistance()) {
nearestBeacon = b;
}
}
PIBeaconData data = new PIBeaconData(nearestBeacon);
data.setDetectedTime(detectedTime);
data.setDeviceDescriptor(mDeviceDescriptor);
beaconArray.add(data.getBeaconAsJson());
payload.put("bnm", beaconArray);
return payload;
}
示例10: addConfigToJSON
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
protected void addConfigToJSON(JSONObject graphConfig, Map<String,Object> config) {
for (String key : config.keySet()) {
Object value = config.get(key);
if (key.equals(ContextProperties.SUBMISSION_PARAMS)) {
// value causes issues below and no need to add this to json
continue;
}
if (JSONObject.isValidObject(value)) {
graphConfig.put(key, value);
continue;
}
if (value instanceof Collection) {
JSONArray ja = new JSONArray();
@SuppressWarnings("unchecked")
Collection<Object> coll = (Collection<Object>) value;
ja.addAll(coll);
graphConfig.put(key, ja);
}
}
}
示例11: apply
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
@Override
public JSONObject apply(List<String> v) {
JSONObject agg = new JSONObject();
JSONArray items = new JSONArray();
for (String e : v)
items.add(e);
agg.put("items", items);
long ts = System.currentTimeMillis();
agg.put("ts", ts);
if (lastts != 0)
agg.put("delta", ts - lastts);
lastts = ts;
agg.put("count", ++count);
return agg;
}
示例12: testDeserializeArray
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
/**
* Test that if the serialized value is
* an array, it ends up wrapped in an object.
*/
@Test
public void testDeserializeArray() throws Exception {
final String data = "[ 100, 500, false, 200, 400 ]";
final Topology t = new Topology();
TStream<String> array = t.strings(data);
TStream<JSONObject> json = JSONStreams.deserialize(array);
TStream<String> jsonString = JSONStreams.serialize(json);
JSONArray ja = (JSONArray) JSON.parse(data);
JSONObject jo = new JSONObject();
jo.put("payload", ja);
checkJsonOutput(jo, jsonString);
}
示例13: testFlatten
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
@Test
public void testFlatten() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
JSONObject e1 = new JSONObject(); e1.put("val", "hello"); array.add(e1);
JSONObject e2 = new JSONObject(); e2.put("val", "goodbye"); array.add(e2);
JSONObject e3 = new JSONObject(); e3.put("val", "farewell"); array.add(e3);
value.put("greetings", array);
List<JSONObject> inputs = new ArrayList<>();
inputs.add(value);
inputs.add(new JSONObject()); // no list present
JSONObject emptyList = new JSONObject();
emptyList.put("greetings", new JSONArray());
inputs.add(emptyList);
TStream<JSONObject> s = t.constants(inputs);
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
completeAndValidate(output, 10, e1.toString(), e2.toString(), e3.toString());
}
示例14: testFlattenNoObjects
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
@Test
public void testFlattenNoObjects() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
array.add("hello");
value.put("greetings", array);
TStream<JSONObject> s = t.constants(Collections.singletonList(value));
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
JSONObject payload = new JSONObject();
payload.put("payload", "hello");
completeAndValidate(output, 10, payload.toString());
}
示例15: WatsonUserModeller
import com.ibm.json.java.JSONArray; //导入依赖的package包/类
public WatsonUserModeller()
{
try {
String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
JSONObject vcap;
vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
watson = (JSONArray) vcap.get("personality_insights");
watsonInstance = (JSONObject) watson.get(0);
watsonCredentials = (JSONObject) watsonInstance.get("credentials");
} catch (IOException e) {
e.printStackTrace();
}
this.username = (String) watsonCredentials.get("username");
this.password = (String) watsonCredentials.get("password");
this.base_url = (String) watsonCredentials.get("url");
this.profile_api = Config.WATSON_PROF_API;
this.visual_api = Config.WATSON_VIZ_API;
this.executor = Executor.newInstance().auth(username, password);
if (this.executor == null)
{
System.err.println("Authentication failed in WatsonUserModeller.");
}
}