本文整理汇总了Java中org.json.JSONObject.get方法的典型用法代码示例。如果您正苦于以下问题:Java JSONObject.get方法的具体用法?Java JSONObject.get怎么用?Java JSONObject.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.json.JSONObject
的用法示例。
在下文中一共展示了JSONObject.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toFlatStringMap
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Convert json object into simple string-string map.
* Non-string values are ignored.
*
* @param object json object to convert
* @return resulting map
* @throws JSONException when json object is invalid
*/
public static Map<String, String> toFlatStringMap(final JSONObject object) throws JSONException {
final Map<String, String> map = new HashMap<String, String>();
final Iterator<String> keysItr = object.keys();
while (keysItr.hasNext()) {
final String key = keysItr.next();
final Object value = object.get(key);
if (!(value instanceof String)) {
continue;
}
map.put(key, (String) value);
}
return map;
}
示例2: update
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Update configurations for a list of keys
*
* @param keys - list of config key to migrate
*/
public static boolean update(List<String> keys) {
try {
Map<String, String> conf = readConfig(keys);
if (conf == null) {
return false;
}
List<String> confSystem = new ArrayList<>(0);
//process
for (String domainId : conf.keySet()) {
xLog.info("Parsing config for domain {0}", domainId);
JSONObject config = new JSONObject(conf.get(domainId));
JSONObject inventory = (JSONObject) config.get("invntry");
JSONObject optmz = (JSONObject) config.get("optmz");
String compute = (String) optmz.get("compute");
if (inventory.has("cr") || inventory.has("dispcr")) {
xLog.info("New configuration already set for: {0}", domainId);
confSystem.add(domainId);
continue;
}
if (optmz.has("compute-crfreqs")) {
optmz.remove("compute-crfreqs");
}
if (inventory.has("manualcr")) {
String manualcr = (String) inventory.get("manualcr");
inventory.put("cr", "0");
inventory.put("manualcrfreq", manualcr);
inventory.put("dispcr", true);
inventory.put("dcrfreq", manualcr);
optmz.put("compute", "-1");
inventory.remove("manualcr");
xLog.info("Configuration set for manualcr: {0}" + domainId);
} else if ("0".equals(compute)) {
inventory.put("cr", "1");
inventory.put("dispcr", false);
optmz.put("compute", "-1");
xLog.info("Configuration set for Automatic: {0}" + domainId);
} else if ("100".equals(compute) || "200".equals(compute)) {
inventory.put("cr", "1");
inventory.put("dispcr", false);
xLog.info("Configuration set for Demand forecast/OOQ: {0}" + domainId);
} else if ("-1".equals(compute)) {
inventory.put("cr", "-1");
inventory.put("dispcr", false);
xLog.info("Configuration set for forecasting as NONE: {0}" + domainId);
}
inventory.put("showpr", false);
config.put("invntry", inventory);
config.put("optmz", optmz);
conf.put(domainId, String.valueOf(config));
}
//update
ps = connection.prepareStatement(CONFIG_UPDATE_QUERY);
for (String confKeys : conf.keySet()) {
if (!confSystem.contains(confKeys)) {
ps.setString(1, conf.get(confKeys));
ps.setString(2, confKeys);
ps.addBatch();
}
}
int[] count = ps.executeBatch();
xLog.info("{0} domains updated out of {1}", count.length, conf.size());
} catch (Exception e) {
xLog.warn("Error in updating configuration: " + e);
return false;
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ignored) {
xLog.warn("Exception while closing prepared statement", ignored);
}
}
}
return true;
}
示例3: build
import org.json.JSONObject; //导入方法依赖的package包/类
public void build(String data){
try {
// PHP에서 받아온 JSON 데이터를 JSON오브젝트로 변환
JSONObject jObject = new JSONObject(data);
// results라는 key는 JSON배열로 되어있다.
JSONArray results = jObject.getJSONArray("result");
String countTemp = (String)jObject.get("num_result");
int count = Integer.parseInt(countTemp);
for ( int i = 0; i < count; ++i ) {
JSONObject temp = results.getJSONObject(i);
convert(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
示例4: deepMerge
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Merge "source" into "target". If fields have equal name, merge them recursively.
*
* @param target On merge conflicts this object loses
* @param source On merge conflicts this object wins
* @return the merged object (target).
* @throws JSONException the json exception
* @source http ://stackoverflow.com/a/15070484/1507692
*/
public static JSONObject deepMerge(final JSONObject target, final JSONObject source) throws JSONException {
final JSONObject newObject = new JSONObject(target.toString());
final Iterator keys = source.keys();
while (keys.hasNext()) {
final String key = keys.next().toString();
final Object value = source.get(key);
if (!newObject.has(key)) {
newObject.put(key, value);
} else {
newObject.put(key, merge(newObject.get(key), value));
}
}
return newObject;
}
示例5: toMap
import org.json.JSONObject; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Map<String, Object> toMap(JSONObject json) throws JSONException {
if (json == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
Object o = json.get(key);
if (o instanceof JSONArray) {
map.put(key, toList((JSONArray) o));
} else if (o instanceof JSONObject) {
map.put(key, toMap((JSONObject) o));
} else {
map.put(key, o);
}
}
return map;
}
示例6: jsonToReact
import org.json.JSONObject; //导入方法依赖的package包/类
public static WritableMap jsonToReact(JSONObject jsonObject) throws JSONException {
WritableMap writableMap = Arguments.createMap();
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()) {
String key = (String) iterator.next();
Object value = jsonObject.get(key);
if (value instanceof Float || value instanceof Double) {
writableMap.putDouble(key, jsonObject.getDouble(key));
} else if (value instanceof Number) {
writableMap.putInt(key, jsonObject.getInt(key));
} else if (value instanceof String) {
writableMap.putString(key, jsonObject.getString(key));
} else if (value instanceof JSONObject) {
writableMap.putMap(key,jsonToReact(jsonObject.getJSONObject(key)));
} else if (value instanceof JSONArray){
writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key)));
} else if (value == JSONObject.NULL){
writableMap.putNull(key);
}
}
return writableMap;
}
示例7: readMap
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Reads a stored mapped from the JSON object and populates the data
* structure
*
* @param <U>
* @param <V>
* @param map
* @param name
* @param key_map
* @param object
* @throws JSONException
*/
@SuppressWarnings("unchecked")
protected <U, V> void readMap(SortedMap<U, V> map, String name, Map<String, U> key_map, Class<?> value_class, JSONObject object) throws JSONException {
map.clear();
JSONObject jsonObject = object.getJSONObject(name);
Iterator<String> keys = jsonObject.keys();
boolean first = true;
while (keys.hasNext()) {
String key_name = keys.next();
U key_object = null;
V value = null;
if (value_class.equals(Long.class)) {
value = (V) new Long(jsonObject.getLong(key_name));
} else {
value = (V) jsonObject.get(key_name);
}
key_object = key_map.get(key_name);
if (key_object == null) {
LOG.warn("Failed to retrieve key object '" + key_name + "' for " + name);
if (LOG.isDebugEnabled() && first) {
LOG.warn(jsonObject.toString(2));
}
first = false;
continue;
}
map.put(key_object, value);
} // FOR
LOG.debug("Added " + map.size() + " values to " + name);
return;
}
示例8: getJSONValue
import org.json.JSONObject; //导入方法依赖的package包/类
private String getJSONValue(JSONObject obj, String propName)
{
String value = null;
if (obj.has(propName)) {
Object obj2 = obj.get(propName);
if (obj2 != null)
value = obj2.toString();
}
return value;
}
示例9: fromJSONString
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* Load the object from the JSON string
*/
public void fromJSONString(String jsonString) throws ProtocolException {
try {
JSONObject json = new JSONObject(jsonString);
// Get the version
this.version = (String) json.get(JsonTagsZ.VERSION);
if ("01".equals(this.version)) {
loadFromJSON01(json);
} else {
throw new ProtocolException("Unsupported protocol version: " + this.version);
}
} catch (JSONException e) {
throw new ProtocolException(e.getMessage());
}
}
示例10: getCellRows
import org.json.JSONObject; //导入方法依赖的package包/类
public int[] getCellRows(JSONObject rowObject) {
JSONArray x = (JSONArray) rowObject.get("rows");
int[] rows = new int[x.length()];
for (int i = 0; i < x.length(); i++) {
rows[i] = x.getInt(i);
}
return rows;
}
示例11: handleErrorResponse
import org.json.JSONObject; //导入方法依赖的package包/类
private void handleErrorResponse(JSONObject response) {
if (response.has("code")) {
if (!(response.get("code") instanceof Integer)) return;
ErrorResponse errorResponse = ErrorResponse.getByKey(response.getInt("code"));
if (errorResponse != ErrorResponse.UNKNOWN) {
throw new ErrorResponseException(errorResponse);
} else {
throw new ErrorResponseException(response.getInt("code"), response.getString("message"));
}
}
}
示例12: putIntentExtra
import org.json.JSONObject; //导入方法依赖的package包/类
/**
* 将jsnon数据传入Intent传递给本地页面
*
* @param jsonObject
* @param intent
* @return
* @throws JSONException
*/
public static Intent putIntentExtra(JSONObject jsonObject, Intent intent) throws JSONException {
if (jsonObject == null || intent == null) {
return null;
}
Iterator<String> it = jsonObject.keys();
while (it.hasNext()) {
String key = it.next();
Object valueObj = jsonObject.get(key);
if (valueObj instanceof Boolean) {
intent.putExtra(key, (boolean) valueObj);
} else if (valueObj instanceof String) {
intent.putExtra(key, valueObj.toString());
} else if (valueObj instanceof Integer) {
intent.putExtra(key, (int) valueObj);
} else if (valueObj instanceof Double) {
intent.putExtra(key, (Double) valueObj);
} else if (valueObj instanceof Float) {
intent.putExtra(key, (Float) valueObj);
} else if (valueObj instanceof Byte) {
intent.putExtra(key, (Byte) valueObj);
} else if (valueObj instanceof Short) {
intent.putExtra(key, (Short) valueObj);
} else if (valueObj instanceof Long) {
intent.putExtra(key, (Long) valueObj);
} else {
intent.putExtra(key, valueObj.toString());
}
}
return intent;
}
示例13: getFromJsonByField2
import org.json.JSONObject; //导入方法依赖的package包/类
/**
*
* @param json
* @param key
* @param toField
* @return
* @throws JSONException
*/
public static Object getFromJsonByField2(JSONObject json, String key, Field toField) throws JSONException {
final Object o = json.get(key);
final Class<?> fieldType = toField.getType();
if (o != JSONObject.NULL) {
if (fieldType.equals(Integer.class) || fieldType.equals(Integer.TYPE)) {
return json.getInt(key);
}
else if (fieldType.equals(String.class)) {
return o;
}
else if (fieldType.equals(Long.class) || fieldType.equals(Long.TYPE)) {
return json.getLong(key);
}
else if (fieldType.equals(Boolean.class) || fieldType.equals(Boolean.TYPE)) {
return json.getBoolean(key);
}
else if (fieldType.equals(Float.class) || fieldType.equals(Float.TYPE)) {
return (float) json.getDouble(key);
}
else if (fieldType.equals(Double.class) || fieldType.equals(Double.TYPE)) {
return json.getDouble(key);
}
else {
return o;
}
}
return JSONObject.NULL;
}
示例14: testJsonFormData
import org.json.JSONObject; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public void testJsonFormData() throws Exception
{
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
JSONObject jsonParsedObject = new JSONObject(new JSONTokener(jsonResponseString));
assertNotNull(jsonParsedObject);
JSONObject rootDataObject = (JSONObject)jsonParsedObject.get("data");
JSONObject formDataObject = (JSONObject)rootDataObject.get("formData");
List<String> keys = new ArrayList<String>();
for (Iterator iter = formDataObject.keys(); iter.hasNext(); )
{
String nextFieldName = (String)iter.next();
assertEquals("Did not expect to find a colon char in " + nextFieldName,
-1, nextFieldName.indexOf(':'));
keys.add(nextFieldName);
}
// Threshold is a rather arbitrary number. I simply want to ensure that there
// are *some* entries in the formData hash.
final int threshold = 5;
int actualKeyCount = keys.size();
assertTrue("Expected more than " + threshold +
" entries in formData. Actual: " + actualKeyCount, actualKeyCount > threshold);
}
示例15: PurchaseSalesOrderConfig
import org.json.JSONObject; //导入方法依赖的package包/类
public PurchaseSalesOrderConfig(JSONObject jsonObject) {
if (jsonObject != null && jsonObject.length() > 0) {
try {
JSONArray jsonArray = jsonObject.getJSONArray(ENTITY_TAGS);
et = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
Object obj = jsonArray.get(i);
et.add((String) obj);
}
} catch (JSONException e) {
et = new ArrayList<>(1);
}
if (jsonObject.get(SALES_ORDER_APPROVAL) != null) {
soa = jsonObject.getBoolean(SALES_ORDER_APPROVAL);
}
if (jsonObject.get(PURCHASE_ORDER_APPROVAL) != null) {
poa = jsonObject.getBoolean(PURCHASE_ORDER_APPROVAL);
}
if (jsonObject.get(SALES_ORDER_APPROVAL_CREATION) != null) {
soac = jsonObject.getBoolean(SALES_ORDER_APPROVAL_CREATION);
}
if (jsonObject.get(SALES_ORDER_APPROVAL_SHIPPING) != null) {
soas = jsonObject.getBoolean(SALES_ORDER_APPROVAL_SHIPPING);
}
}
}