本文整理汇总了Java中org.json.JSONObject.names方法的典型用法代码示例。如果您正苦于以下问题:Java JSONObject.names方法的具体用法?Java JSONObject.names怎么用?Java JSONObject.names使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.json.JSONObject
的用法示例。
在下文中一共展示了JSONObject.names方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeNullFields
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Remove 'null' fields from a JSONObject. This method calls itself recursively until all the
* fields have been looked at.
* TODO(mlamouri): move to some util class?
*/
private static void removeNullFields(Object object) throws JSONException {
if (object instanceof JSONArray) {
JSONArray array = (JSONArray) object;
for (int i = 0; i < array.length(); ++i) removeNullFields(array.get(i));
} else if (object instanceof JSONObject) {
JSONObject json = (JSONObject) object;
JSONArray names = json.names();
if (names == null) return;
for (int i = 0; i < names.length(); ++i) {
String key = names.getString(i);
if (json.isNull(key)) {
json.remove(key);
} else {
removeNullFields(json.get(key));
}
}
}
}
示例2: convertJSONObjectToHashMap
import org.json.JSONObject; //导入方法依赖的package包/类
static Map<String, Object> convertJSONObjectToHashMap(JSONObject jsonObject) {
HashMap<String, Object> map = new HashMap<String, Object>();
JSONArray keys = jsonObject.names();
for (int i = 0; i < keys.length(); ++i) {
String key;
try {
key = keys.getString(i);
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
value = convertJSONObjectToHashMap((JSONObject) value);
}
map.put(key, value);
} catch (JSONException e) {
}
}
return map;
}
示例3: getInfo
import org.json.JSONObject; //导入方法依赖的package包/类
public List<Map<String, String>> getInfo(Context context, String key) {
List<Map<String, String>> datas = new ArrayList<>();
SharedPreferences sp = context.getSharedPreferences("HistoricalRecord", Context.MODE_PRIVATE);
String result = sp.getString(key, "");
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject itemObject = array.getJSONObject(i);
Map<String, String> itemMap = new HashMap<>();
JSONArray names = itemObject.names();
if (names != null) {
for (int j = 0; j < names.length(); j++) {
String name = names.getString(j);
String value = itemObject.getString(name);
itemMap.put(name, value);
}
}
datas.add(itemMap);
}
} catch (JSONException e) {
e.printStackTrace();
}
return datas;
}
示例4: parseMap
import org.json.JSONObject; //导入方法依赖的package包/类
private void parseMap(JSONObject mapObject, Map<String, String> target)
{
JSONArray keys = mapObject.names();
for (int i = 0; i < keys.length(); i++)
{
try
{
String key = (String) keys.get(i);
String value = mapObject.getString(key);
target.put(key, value);
} catch (JSONException e)
{
System.err.println("Error parsing " + mapObject);
e.printStackTrace(System.err);
}
}
}
开发者ID:pivotal-partner-solution-architecture,项目名称:azure-service-broker-client,代码行数:18,代码来源:VcapParser.java
示例5: removeUnusedArticleProperties
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Removes unused properties of each article.
*
* @param context
* the specified context
*/
@RequestMapping(value = "/fix/normalization/articles/properties", method = RequestMethod.POST)
public void removeUnusedArticleProperties(final HttpServletRequest request, final HttpServletResponse response) {
logger.info("Processes remove unused article properties");
final TextHTMLRenderer renderer = new TextHTMLRenderer();
// Transaction transaction = null;
try {
final JSONArray articles = articleDao.get(new Query()).getJSONArray(Keys.RESULTS);
if (articles.length() <= 0) {
renderer.setContent("No unused article properties");
renderer.render(request, response);
return;
}
// transaction = articleDao.beginTransaction();
final Set<String> keyNames = Repositories.getKeyNames(Article.ARTICLE);
for (int i = 0; i < articles.length(); i++) {
final JSONObject article = articles.getJSONObject(i);
final JSONArray names = article.names();
final Set<String> nameSet = CollectionUtils.jsonArrayToSet(names);
if (nameSet.removeAll(keyNames)) {
for (final String unusedName : nameSet) {
article.remove(unusedName);
}
articleDao.update(article.getString(Keys.OBJECT_ID), article);
logger.info("Found an article[id={}] exists unused properties[{}]",
article.getString(Keys.OBJECT_ID), nameSet);
}
}
// transaction.commit();
} catch (final Exception e) {
// if (null != transaction && transaction.isActive()) {
// transaction.rollback();
// }
logger.error(e.getMessage(), e);
renderer.setContent("Removes unused article properties failed, error msg[" + e.getMessage() + "]");
}
renderer.render(request, response);
}
示例6: readSpeedValues
import org.json.JSONObject; //导入方法依赖的package包/类
private void readSpeedValues(JSONObject json, String keyName, Map<String, Integer> speeds)
{
if (json.has(keyName))
{
JSONObject jLimits = json.getJSONObject(keyName);
JSONArray jKeys = jLimits.names();
for(int i = 0; i < jKeys.length(); i++)
{
String key = jKeys.getString(i);
speeds.put(key.toLowerCase(), jLimits.getInt(key));
}
}
}
示例7: addToJSONObject
import org.json.JSONObject; //导入方法依赖的package包/类
public static void addToJSONObject(final JSONObject data, final JSONObject additionalValues) throws JSONException
{
if (additionalValues != null && additionalValues.length() > 0)
{
final JSONArray attr = additionalValues.names();
for (int i = 0; i < attr.length(); i++)
data.put(attr.getString(i), additionalValues.get(attr.getString(i)));
}
}
示例8: parse
import org.json.JSONObject; //导入方法依赖的package包/类
public VcapResult parse(String vcapServices) {
final VcapResult result = new VcapResult();
final List<VcapPojo> results = new ArrayList<>();
log("VcapParser.parse: vcapServices = " + vcapServices);
if (vcapServices != null) {
try {
final JSONObject json = new JSONObject(vcapServices);
final JSONArray names = json.names();
for (int i = 0; i < names.length(); i++) {
final String name = (String) names.get(i);
if (name.startsWith(AZURE)) {
final JSONArray azureService = json.getJSONArray(name);
final int numElements = azureService.length();
for (int index = 0; index < numElements; index++) {
final VcapPojo pojo = parseService(name, azureService,
vcapServices, index);
results.add(pojo);
}
}
}
} catch (JSONException e) {
System.err.println("Error parsing " + vcapServices);
e.printStackTrace(System.err);
}
}
result.setPojos(results.toArray(new VcapPojo[results.size()]));
return result;
}
示例9: parserScope
import org.json.JSONObject; //导入方法依赖的package包/类
private Bundle parserScope(String scope) {
Exception e;
if (TextUtils.isEmpty(scope)) {
LOG.e(TAG, "[scope:" + scope + "] response is empty");
return null;
}
if (scope.startsWith("[") && scope.endsWith("]")) {
scope = scope.substring(1, scope.length() - 1);
}
Bundle bundle = null;
try {
Bundle bundle2 = new Bundle();
try {
JSONObject jo = new JSONObject(scope);
JSONArray ja = jo.names();
for (int i = 0; i < ja.length(); i++) {
String name = ja.getString(i);
KVUtils.put(bundle2, name, jo.getString(name));
}
bundle = bundle2;
} catch (Exception e2) {
e = e2;
bundle = bundle2;
LOG.e(TAG, "json parse failed(Exception)", e);
LOG.d(TAG, "bundle : " + bundle.toString());
return bundle;
}
} catch (Exception e3) {
e = e3;
LOG.e(TAG, "json parse failed(Exception)", e);
LOG.d(TAG, "bundle : " + bundle.toString());
return bundle;
}
LOG.d(TAG, "bundle : " + bundle.toString());
return bundle;
}
示例10: buildReportTableData
import org.json.JSONObject; //导入方法依赖的package包/类
@Override
public TableResponseModel buildReportTableData(String json, ReportViewType viewType,
QueryRequestModel model) throws Exception {
JSONObject jsonObject = new JSONObject(json);
if (!jsonObject.has(HEADINGS)) {
xLogger.warn("No data found");
return null;
}
JSONArray headersJson = jsonObject.getJSONArray(HEADINGS);
if (headersJson.length() < 3) {
xLogger.warn("Insufficient data found. Expect to have at least 3 columns");
return null;
}
jsonObject = constructTableBaseData(jsonObject, viewType, headersJson, model);
if (jsonObject.has(TABLE) && jsonObject.getJSONObject(TABLE).length() != 0) {
TableResponseModel response = new TableResponseModel();
JSONArray jsonArray = jsonObject.getJSONArray(HEADINGS);
JSONArray labelJsonArray = jsonObject.getJSONArray(LABEL_HEADINGS);
for (int i = 0; i < labelJsonArray.length(); i++) {
response.headings.add(labelJsonArray.getString(i));
}
List<Field> fields = new ArrayList<>(headersJson.length());
for (int j = 0; j < headersJson.length(); j++) {
fields.add(Report.class.getDeclaredField(headersJson.getString(j)));
}
JSONObject results = jsonObject.getJSONObject(TABLE);
Map<String, List<List<ReportDataModel>>> tables = new HashMap<>(results.length());
JSONArray dimensions = results.names();
boolean isMatInvolved = (viewType == ReportViewType.BY_MATERIAL
|| model.filters.containsKey(QueryHelper.TOKEN + QueryHelper.QUERY_MATERIAL));
boolean isKioskInvolved = (viewType == ReportViewType.BY_ENTITY
|| model.filters.containsKey(QueryHelper.TOKEN + QueryHelper.QUERY_ENTITY));
for (int i = 0; i < dimensions.length(); i++) {
JSONArray period = results.getJSONArray(dimensions.getString(i));
String key = getTableKeyByViewType(viewType, dimensions.getString(i));
tables.put(key, new ArrayList<List<ReportDataModel>>(period.length()));
List<Report> reports = new ArrayList<>();
for (int j = 0; j < period.length(); j++) {
JSONArray row = period.getJSONArray(j);
Report r = constructReport(fields.subList(2, fields.size()), row);
r.setTime(jsonArray.getString(j + 1));
reports.add(r);
}
for (Report report : reports) {
tables.get(key).add(getReportValues(report, ReportCompareField.NONE,
isMatInvolved, isKioskInvolved));
}
}
response.table = tables;
return response;
}
return null;
}
示例11: loadUrl
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Load the url into the webview.
*
* @param url
* @param props Properties that can be passed in to the Cordova activity (i.e. loadingDialog, wait, ...)
* @throws JSONException
*/
public void loadUrl(String url, JSONObject props) throws JSONException {
LOG.d("App", "App.loadUrl("+url+","+props+")");
int wait = 0;
boolean openExternal = false;
boolean clearHistory = false;
// If there are properties, then set them on the Activity
HashMap<String, Object> params = new HashMap<String, Object>();
if (props != null) {
JSONArray keys = props.names();
for (int i = 0; i < keys.length(); i++) {
String key = keys.getString(i);
if (key.equals("wait")) {
wait = props.getInt(key);
}
else if (key.equalsIgnoreCase("openexternal")) {
openExternal = props.getBoolean(key);
}
else if (key.equalsIgnoreCase("clearhistory")) {
clearHistory = props.getBoolean(key);
}
else {
Object value = props.get(key);
if (value == null) {
}
else if (value.getClass().equals(String.class)) {
params.put(key, (String)value);
}
else if (value.getClass().equals(Boolean.class)) {
params.put(key, (Boolean)value);
}
else if (value.getClass().equals(Integer.class)) {
params.put(key, (Integer)value);
}
}
}
}
// If wait property, then delay loading
if (wait > 0) {
try {
synchronized(this) {
this.wait(wait);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.webView.showWebPage(url, openExternal, clearHistory, params);
}
示例12: isEmptyObject
import org.json.JSONObject; //导入方法依赖的package包/类
public static boolean isEmptyObject(JSONObject object) {
return object.names() == null;
}
示例13: checkExtraConsistency
import org.json.JSONObject; //导入方法依赖的package包/类
private void checkExtraConsistency(Response response)
{
JSONObject jResponse = new JSONObject(response.body().asString());
JSONObject jRoute = (jResponse.getJSONArray("routes")).getJSONObject(0);
double routeDistance = jRoute.getJSONObject("summary").getDouble("distance");
JSONObject jExtras = (jResponse.getJSONArray("routes")).getJSONObject(0).getJSONObject("extras");
JSONArray jExtraNames = jExtras.names();
for(int i = 0; i < jExtraNames.length(); i++)
{
String name = jExtraNames.getString(i);
JSONArray jExtraValues = jExtras.getJSONObject(name).getJSONArray("values");
JSONArray jValues = jExtraValues.getJSONArray(0);
int fromValue = jValues.getInt(0);
int toValue = jValues.getInt(1);
Assert.assertEquals(fromValue < toValue, true);
for(int j = 1; j < jExtraValues.length(); j++)
{
jValues = jExtraValues.getJSONArray(j);
int fromValue1 = jValues.getInt(0);
int toValue1 = jValues.getInt(1);
Assert.assertEquals(fromValue1 < toValue1, true);
Assert.assertEquals(fromValue1 == toValue, true);
fromValue = fromValue1;
toValue = toValue1;
}
JSONArray jSummary = jExtras.getJSONObject(name).getJSONArray("summary");
double distance = 0.0;
double amount = 0.0;
for(int j = 0; j < jSummary.length(); j++)
{
JSONObject jSummaryValues = jSummary.getJSONObject(j);
distance += jSummaryValues.getDouble("distance");
amount += jSummaryValues.getDouble("amount");
}
Assert.assertEquals(Math.abs(routeDistance - distance) < 0.5, true);
Assert.assertEquals(Math.abs(amount - 100.0) < 0.1, true);
}
}
示例14: parseJsonParameterValues
import org.json.JSONObject; //导入方法依赖的package包/类
protected Map<String, Serializable> parseJsonParameterValues(JSONObject jsonParameterValues, String name, boolean isAction) throws JSONException
{
Map<String, Serializable> parameterValues = new HashMap<String, Serializable>();
// get parameters names
JSONArray names = jsonParameterValues.names();
if (names == null)
{
return null;
}
// Get the action or condition definition
ParameterizedItemDefinition definition = null;
if (isAction == true)
{
definition = actionService.getActionDefinition(name);
}
else
{
definition = actionService.getActionConditionDefinition(name);
}
if (definition == null)
{
throw new AlfrescoRuntimeException("Could not find defintion for action/condition " + name);
}
for (int i = 0; i < names.length(); i++)
{
String propertyName = names.getString(i);
Object propertyValue = jsonParameterValues.get(propertyName);
// Get the parameter definition we care about
ParameterDefinition paramDef = definition.getParameterDefintion(propertyName);
if (paramDef == null && !definition.getAdhocPropertiesAllowed())
{
throw new AlfrescoRuntimeException("Invalid parameter " + propertyName + " for action/condition " + name);
}
if (paramDef != null)
{
QName typeQName = paramDef.getType();
// Convert the property value
Serializable value = convertValue(typeQName, propertyValue);
parameterValues.put(propertyName, value);
}
else
{
// If there is no parameter definition we can only rely on the .toString() representation of the ad-hoc property
parameterValues.put(propertyName, propertyValue.toString());
}
}
return parameterValues;
}
示例15: buildReportTableData
import org.json.JSONObject; //导入方法依赖的package包/类
@Override
public TableResponseModel buildReportTableData(String json, ReportViewType viewType,
QueryRequestModel model) throws Exception {
JSONObject jsonObject = new JSONObject(json);
if (!jsonObject.has(HEADINGS)) {
xLogger.warn("No data found");
return null;
}
JSONArray headersJson = jsonObject.getJSONArray(HEADINGS);
if (headersJson.length() < 3) {
xLogger.warn("Insufficient data found. Expect to have at least 3 columns");
return null;
}
jsonObject = constructTableBaseData(jsonObject, viewType, headersJson, model);
if (jsonObject.has(TABLE) && jsonObject.getJSONObject(TABLE).length() != 0) {
TableResponseModel response = new TableResponseModel();
JSONArray jsonArray = jsonObject.getJSONArray(HEADINGS);
JSONArray labelJsonArray = jsonObject.getJSONArray(LABEL_HEADINGS);
for (int i = 0; i < labelJsonArray.length(); i++) {
response.headings.add(labelJsonArray.getString(i));
}
List<Field> fields = new ArrayList<>(headersJson.length());
for (int j = 0; j < headersJson.length(); j++) {
fields.add(Report.class.getDeclaredField(headersJson.getString(j)));
}
JSONObject results = jsonObject.getJSONObject(TABLE);
Map<String, List<List<ReportDataModel>>> tables = new HashMap<>(results.length());
JSONArray dimensions = results.names();
for (int i = 0; i < dimensions.length(); i++) {
JSONArray period = results.getJSONArray(dimensions.getString(i));
String key = getTableKeyByViewType(viewType, dimensions.getString(i));
tables.put(key, new ArrayList<List<ReportDataModel>>(period.length()));
List<Report> reports = new ArrayList<>();
for (int j = 0; j < period.length(); j++) {
JSONArray row = period.getJSONArray(j);
Report r = constructReport(fields.subList(2, fields.size()), row);
r.setTime(jsonArray.getString(j + 1));
reports.add(r);
}
for (Report report : reports) {
tables.get(key).add(getReportValues(report, ReportCompareField.NONE));
}
}
response.table = tables;
return response;
}
return null;
}