Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据属性过滤JSON字符串数组?_Java_Json_Gson - Fatal编程技术网

Java 如何根据属性过滤JSON字符串数组?

Java 如何根据属性过滤JSON字符串数组?,java,json,gson,Java,Json,Gson,我有一个JSON字符串数组,其中它的条目具有以下属性:firstName、lastName、loginName、Country、phoneNumber和status。这里有一个例子 [ { "firstName": "Patrick", "lastName": "Smith", "loginName":"test0003@test.com", "Country":"US", "phoneNumber"

我有一个JSON字符串数组,其中它的条目具有以下属性:firstName、lastName、loginName、Country、phoneNumber和status。这里有一个例子

[  
    {  
        "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"
    }
]
现在,我想根据属性loginName和status搜索特定条目

比如说

  • 登录名:test0001@test.com
  • 状态:167

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

最优化的解决方案是什么?

使用Jackson,这是我能想到的最粗糙的片段:

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;
}

当然,它只会将第一个匹配返回给给定的对。如果您需要所有的值,请让它返回一个列表,并在循环中填充它。

我使用JSONObject,这里是另一种解析方法。 1.使用
JSONArray进行解析
2.循环数组并读入UserProfile对象
3.将其存储在HashMap中以使用密钥获取

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());
    }

  }
}
import java.util.HashMap;
导入java.util.Map;
导入org.json.*;
类用户配置文件{
字符串登录名;
智力状态;
字符串名;
字符串键;
UserProfile(){
}
字符串getLoginName(){
返回登录名;
}
字符串getFirstName(){
返回名字;
}
无效设置键(字符串键){
this.key=key;
}
void setLoginName(字符串loginName){
this.loginName=loginName;
}
无效设置状态(int状态){
这个状态=状态;
}
void setFirstName(字符串firstName){
this.firstName=firstName;
}
}
公共类JSONObjParser{
公共静态void main(字符串[]args){
Map Map=newhashmap();
String msg=“[{firstName:Patrick,lastName:Smith,loginName:test0003@test.com,国家:美国,电话号码:287125-1434,状态:340},{姓:鲍勃,姓:威廉姆斯,登录名:test0002@test.com,国家:美国,电话号码:213 111-9943,状态:215}];
JSONArray JSONArray=新JSONArray(msg);
for(int i=0;i
Google“java json”并选择一个库。一旦你试着使用它,但被卡住了,再问一次。在这里写这句话就像在麦当劳说“我想要一份沙拉”。。。我的意思是,这里不是这样的地方。@efthialex我听起来像个傻瓜,但没什么好解释的。您的json数组只是json对象的集合,我使用Jackson将其解析并表示为对象(映射集合)。有了这些对象,我们可以使用默认的MapAPI来搜索我们想要的条目。给定的代码段可能看起来足够通用,可以与其他JSON一起使用,但情况并非如此,因为它要求您始终搜索2个值。这就是我说它很粗糙的原因,还有很多改进的空间。