得到关键名称&来自json的关键价值

时间:2015-09-30 21:23:45

标签: 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;
}

更新

现在我收到了错误:

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;
}

2 个答案:

答案 0 :(得分: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));
}

答案 1 :(得分:2)

由于您的Java和JSON新手是最着名的数据交换语言之一,我建议您彻底了解JSON的解析和结构this example

GooglePlus.login().then(function(response) { GooglePlus.getUser().then(function(response) { console.log(response); }); }, function(err) { console.log(err); }); };

这将获取JSON中的一组键。