How to filter JSON String array based on attributes?

只谈情不闲聊 提交于 2020-01-06 23:51:04

问题


I have a JSON-String array, where its entry has the following properties, firstName, lastName, loginName, Country, phoneNumber, and status. Here's an example

[  
    {  
        "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": "167"
    },
    {  
        "firstName": "George",
        "lastName": "Jones",
        "loginName":"test0004@test.com",
        "Country":"FR",
        "phoneNumber": "217 987-2634",
        "status": "340"
    }
]

Now, I want to search for a specific entry based on the properties loginName and status

For example

  • loginName: test0001@test.com
  • status: 167

    {  
        "firstName": "John",
        "lastName": "Johnson",
        "loginName":"test0001@test.com",
        "Country":"DE",
        "phoneNumber": "212 555-1234",
        "status": "167"
    }
    

What would be the most optimized solution?


回答1:


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.




回答2:


I use JSONObject and here is another way to parse. 1. Parse using JSONArray 2. Loop the array and read into UserProfile objects 3. Store it in HashMap to get using key

import java.util.HashMap;
import java.util.Map;
import org.json.*;

class UserProfile{
  String loginName;
  int status;
  String firstName;
  String key;

  UserProfile(){

  }

  String getLoginName(){
    return loginName;
  }

  String getFirstName(){
    return firstName;
  }

  void setKey(String key){
    this.key = key;
  }

  void setLoginName(String loginName){
    this.loginName = loginName;
  }

  void setStatus(int status){
    this.status = status;
  }

  void setFirstName(String firstName){
    this.firstName = firstName;
  }
}

public class JSONObjParser {

  public static void main(String[] args){
    Map<String, UserProfile> map = new HashMap<String, UserProfile>();

    String msg ="[{ 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 }]";
    JSONArray jsonarray = new JSONArray(msg);
    for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        String loginName = jsonobject.getString("loginName");
        int status = jsonobject.getInt("status");
        String firstName = jsonobject.getString("firstName");

        UserProfile profile = new UserProfile();
        profile.setFirstName(firstName);
        profile.setLoginName(loginName);
        profile.setStatus(status);
        String key = loginName + Integer.toString(status);
        map.put(key, profile);
    }

    for (String key : map.keySet()) {
        UserProfile profile = map.get(key);
        System.out.println("Key = " + key + ", FirstName = " + profile.getFirstName());
    }

  }
}


来源:https://stackoverflow.com/questions/36749119/how-to-filter-json-string-array-based-on-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!