如何过滤基于属性的JSON字符串数组? [英] How to filter JSON String array based on attributes?
本文介绍了如何过滤基于属性的JSON字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON-String数组
[
{
firstName: Patrick,
lastName:Smith,
loginName:test0003@test.com,
国家:美国,
phoneNumber :287 125-1434,
status:340
},
{
firstName:Bob,
lastName: Williams,
loginName:test0002@test.com,
国家:美国,
phoneNumber:213 111-9943,
status:215
},
{
firstName:John,
lastName:Johnson,
loginName :test0001@test.com,
国家:DE,
phoneNumber:212 555-1234,
status:340
$,
{
firstName:John,
lastName:Johnson,
loginName:test0001@test.com,
国家:DE,
phoneNumber:212 555-1 234,
status:215
},
{
firstName:John,
lastName:Johnson,
loginName:test0001@test.com,
国家:DE,
phoneNumber:212 555-1234,
status: 167
},
{
firstName:George,
lastName:Jones,
loginName:test0004 @ test .com,
国家:FR,
phoneNumber:217 987-2634,
status:340
}
]
我只需要返回这部分
{
firstName:John,
lastName:Johnson,
loginName:test0001 @ test .com,
Country:DE,
phoneNumber:212 555-1234,
status:167
} $ b $通过为 loginName 和状态
>
我需要cr给出一个如下所示的方法。
public String filterJsonArray(String array,String keyOne,Object valueOne,String keyTwo,Object valueTwo )抛出ParseException {
}
有人能告诉我如何我可以实现这一点。
解决方案使用Jackson,这是我能想到的最粗略的片段:
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String [] args)抛出IOException {
System.out.println(filterJsonArray(JSON,loginName,test0001@test.com,status ,167));
$ b $ public static String filterJsonArray(String array,String keyOne,Object valueOne,String keyTwo,Object valueTwo)throws IOException {
Map [] nodes = mapper.readValue(array ,HashMap []。class); (节点:节点){
if(node.containsKey(keyOne)&& node.containsKey(keyTwo)){
if(node.get( keyOne).equals(valueOne)&& node.get(keyTwo).equals(valueTwo)){
return mapper.writeValueAsString(node);
}
}
}
返回null;
}
当然,它只会返回给定对的第一个匹配。如果你需要所有的值,让它返回一个列表,并将其填充到循环中。
I have the following JSON-String array
[
{
"firstName": "Patrick",
"lastName": "Smith",
"loginName":"test0003@test.com",
"Country":"US",
"phoneNumber": "287 125-1434",
"status": "340"
},
{
"firstName": "Bob",
"lastName": "Williams",
"loginName":"test0002@test.com",
"Country":"US",
"phoneNumber": "213 111-9943",
"status": "215"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "340"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "215"
},
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "167"
},
{
"firstName": "George",
"lastName": "Jones",
"loginName":"test0004@test.com",
"Country":"FR",
"phoneNumber": "217 987-2634",
"status": "340"
}
]
and I need to return only this part
{
"firstName": "John",
"lastName": "Johnson",
"loginName":"test0001@test.com",
"Country":"DE",
"phoneNumber": "212 555-1234",
"status": "167"
}
by providing values for loginName and status
I need to create a method as the one below.
public String filterJsonArray(String array, String keyOne, Object valueOne, String keyTwo, Object valueTwo) throws ParseException {
}
Can someone give me an idea of how I could implement this.
解决方案
Using Jackson, this is the crudest snippet I can think of:
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
System.out.println(filterJsonArray(JSON, "loginName", "test0001@test.com", "status", "167"));
}
public static String filterJsonArray(String array, String keyOne, Object valueOne, String keyTwo, Object valueTwo) throws IOException {
Map[] nodes = mapper.readValue(array, HashMap[].class);
for (Map node : nodes) {
if (node.containsKey(keyOne) && node.containsKey(keyTwo)) {
if (node.get(keyOne).equals(valueOne) && node.get(keyTwo).equals(valueTwo)) {
return mapper.writeValueAsString(node);
}
}
}
return null;
}
Of course it will only returns the first match to the given pairs. If you need all the values, make it return a list instead and populate it inside the loop.
这篇关于如何过滤基于属性的JSON字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文