如何从 java 中的 Map 键获取值
How to get value from Map key in java
我正在尝试使用以下代码从 Amazon SQS 接收消息属性:
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));
}
它returns输出:
project - {StringValue: 25,StringListValues: [],BinaryListValues: [],DataType: String}
我只想获取值 25。我该怎么做?
因为这是在您的 Map
中声明的值类型 com.amazonaws.services.sqs.model.MessageAttributeValue
的对象。
您需要像往常一样从该对象中获取值:
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());
}
您需要在您的地图上循环,这里是一个向您展示如何获取地图值的示例:
我有一个: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());
}
使用这个(库 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();
}
}
试试这个:
attributes.get("project").getStringValue()
参考文献:
我正在尝试使用以下代码从 Amazon SQS 接收消息属性:
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));
}
它returns输出:
project - {StringValue: 25,StringListValues: [],BinaryListValues: [],DataType: String}
我只想获取值 25。我该怎么做?
因为这是在您的 Map
中声明的值类型 com.amazonaws.services.sqs.model.MessageAttributeValue
的对象。
您需要像往常一样从该对象中获取值:
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());
}
您需要在您的地图上循环,这里是一个向您展示如何获取地图值的示例:
我有一个: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());
}
使用这个(库 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();
}
}
试试这个:
attributes.get("project").getStringValue()
参考文献: