【发布时间】:2018-07-14 11:09:35
【问题描述】:
这是对特定查询的响应。所以我需要获取每个值来检查值。但是我很困惑在 hashmap 中获取每个值。对于每个值,比如好,坏人数增加。那么我怎样才能在哈希图中得到它。
{
"Great": [
{
"name": "angitha",
"number": "2",
"value": "nice"
},
{
"name": "shiva",
"number": "53",
"value": "it"
}
],
"Okay": [
{
"name": "anita",
"number": "2",
"value": "nice"
},
{
"name": "hansika",
"number": "21",
"value": "nice"
},
{
"name": "angitha",
"number": "24",
"value": "wow"
],
"Bad": [
{
"name": "varun",
"number": "266",
"value": "niceee"
}
]
}
我试过这个。反馈大小变成了三个(很好,好的,坏...)。但是值没有得到......得到空值。而且我是 java hashmap 的新手。
public void comments() throws Exception {
Map feedback = value.getBody().jsonPath().get();
for (int i = 0; i < feedback.size(); i++) {
List comments = (List) feedback.get(i);
for (int j = 0; j< comments.size(); i++) {
Map commentseaching = (Map) comments.get(i);
Assert.assertTrue(!commentseaching.get("name").toString().isEmpty());
Assert.assertEquals(commentseach.get("value").toString());
Assert.assertTrue(!commentseaching.get("number").toString().isEmpty());
}
}
}
我的代码
这段代码运行得很好。但是这里我取的是基于 get("great") 这样的值。有没有其他方法可以减少重复。
public void comments() throws Exception{
Map feedback = value.getBody().jsonPath().get();
List comments = (List) feedbackComments.get("Great");
for(int i=0; i<comments.size();i++){
Map commentseaching = (Map) comments.get(i);
Assert.assertTrue(!commentseaching.get("name").toString().isEmpty());
Assert.assertEquals(commentseaching.get("value").toString());
Assert.assertTrue(!commentseaching.get("number").toString().isEmpty());
}
comments=(List) feedbackComments.get("Okay");
for(int i=0; i<comments.size();i++){
Map commentseaching = (Map) comments.get(i);
Assert.assertTrue(!commentseaching.get("name").toString().isEmpty());
Assert.assertEquals(commentseaching.get("value").toString());
Assert.assertTrue(!commentseaching.get("number").toString().isEmpty());
}
comments=(List) feedbackComments.get("Bad");
for(int i=0; i<comments.size();i++){
Map commentseaching = (Map) comments.get(i);
Assert.assertTrue(!commentseaching.get("name").toString().isEmpty());
Assert.assertEquals(commentseaching.get("value").toString());
Assert.assertTrue(!commentseaching.get("number").toString().isEmpty());
}
}
【问题讨论】:
-
第28行的json数据有问题,必须是
} ], "Bad": -
老实说,我建议不要将这些放在 HashMap 中,而是放在 POJO 中。