如何从java中的Map键获取值

时间:2016-05-11 13:28:30

标签: java string amazon-web-services dictionary key

我正在尝试使用以下代码从Amazon SQS接收消息属性:

    //Open existing scorecard
    $scope.openScorecard = function(){$mdDialog.show({
        templateUrl: 'templates/scorecard.tmpl.html',
        parent: angular.element(document.body),
        clickOutsideToClose: true,
            controller: function($scope, $mdDialog) {
            $scope.cancel = function(){
                $mdDialog.cancel();
            }

            $scope.selectMonth = function(){
                $scope.editState = false;
                $http({
                    method: 'POST',
                    url: 'php/saveComments.php',
                    data : employee
                })
            }

            $scope.save = function(){
                $scope.editState = false;
                $http({
                    method: 'POST',
                    url: 'php/saveComments.php',
                    data : employee
                })
            }

            $scope.hide = function(){
                $mdDialog.hide();
            }
        }
     })
   }
}])

并返回输出:

Map<String, com.amazonaws.services.sqs.model.MessageAttributeValue> attributes = new HashMap<String, com.amazonaws.services.sqs.model.MessageAttributeValue>();
attributes = message.getMessageAttributes();
for(String key: attributes.keySet()){
    System.out.println(key + " - "+ attributes.get(key));
}

我想只得到25分。我该怎么做?

4 个答案:

答案 0 :(得分:1)

因为这是com.amazonaws.services.sqs.model.MessageAttributeValue中声明的值类型Map的对象。

您需要像通常那样从该对象中获取值:

for(String key: attributes.keySet()){
   com.amazonaws.services.sqs.model.MessageAttributeValue object =  attributes.get(key);
   //some method to get that 25 value
   System.out.println(key + " - "+ object.getStringValue());

}

答案 1 :(得分:1)

试试这个:

attributes.get("project").getStringValue()

参考文献:

答案 2 :(得分:0)

您需要在地图上循环这里是一个示例,向您展示如何获取地图的值: 我有一个:Map<String, Object> map = new HashMap<String, Object>();

 for (Entry<String, Object> entry : map.entrySet()) {

                     System.out.println("entry key : "+entry.getKey());
                     System.out.println("Object value :"+entry.getValue());
                  }

答案 3 :(得分:0)

使用它(库org.json.JSONObject; [java-json.jar])

Map<String, com.amazonaws.services.sqs.model.MessageAttributeValue> attributes = new HashMap<String, com.amazonaws.services.sqs.model.MessageAttributeValue>();
        attributes = message.getMessageAttributes();
        for(String key: attributes.keySet()){
            System.out.println(key + " - "+ attributes.get(key));
            try {
                JSONObject json = new JSONObject(attributes.get(key));
                System.out.println(json.get("StringValue"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }