从json获取密钥名称和密钥值

Jus*_*ung 6 java json cordova

我是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;
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

现在我收到了错误:

Object cannot be converted to Number  
Number value = json_array.get(key);
Run Code Online (Sandbox Code Playgroud)

-

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;
}
Run Code Online (Sandbox Code Playgroud)

小智 12

试试这个

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));
}
Run Code Online (Sandbox Code Playgroud)