我是 java 的新手。如何获取此 jsonobject 的键名和键值并将其传递给我的增量方法?
args = {'property_name':1}
private boolean handlePeopleIncrement(JSONArray args, final CallbackContext cbCtx) {
JSONObject json_array = args.optJSONObject(0);
mixpanel.getPeople().increment(key_name, key_value);
cbCtx.success();
return true;
}
更新
现在我得到了错误:
Object cannot be converted to Number
Number value = json_array.get(key);
-
private boolean handlePeopleIncrement(JSONArray args, final CallbackContext cbCtx) {
JSONObject json_array = args.optJSONObject(0);
Iterator<?> keys = json_array.keys();
while( keys.hasNext() ) {
String key = (String) keys.next();
Number value = json_array.get(key);
// System.out.println("Key: " + key);
// System.out.println("Value: " + json_array.get(key));
}
mixpanel.getPeople().increment(key, value);
cbCtx.success();
return true;
}
最佳答案
尝试使用这个
JSONObject json_array = args.optJSONObject(0);
Iterator<?> keys = json_array.keys();
while( keys.hasNext() ) {
String key = (String) keys.next();
System.out.println("Key: " + key);
System.out.println("Value: " + json_array.get(key));
}
关于java - 从 json 获取键名和键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875874/