GSON / JSON - 如果两个属性值匹配返回Object

时间:2017-01-13 14:03:08

标签: java json gson

我正在寻找一个JSON对象数组,如果两个属性与输入的值匹配,那么我希望返回该对象。目前,如果一个属性或另一个属性匹配,我的代码将返回,而我只希望在两者匹配时返回该对象。

我的方法:

 ArrayList<JsonObject> searchAttributes(String sensor, List<String> attributes, List<String> values) throws IOException {
   establishPastEventConnection(sensor);
   JsonParser jp = new JsonParser();
   JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
   JsonArray rootArr = root.getAsJsonArray();
   JsonObject rootObj;
   ArrayList<JsonObject> results = new ArrayList<>();
   for (int x = 0; x < root.getAsJsonArray().size(); x++) {
     rootObj = rootArr.get(x).getAsJsonObject();
     for (String attribute : attributes) {
       for (String value : values) {
         if (rootObj.get(attribute).getAsString().equals(value)) {
               results.add(rootObj);
         }
       }
     }
   }
   return results;
 }

方法调用:

nexaConnect.searchAttributes("13619978", Arrays.asList("timeStamp", "eventCode"), Arrays.asList("1.483384638619905E9", "1"))

示例JSON:

[
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483384640123117E9,"uID":"136199_3_10"},
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483379834470379E9,"uID":"136199_3_10"},
  {"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"136199","timeStamp":1.483384639621985E9,"uID":"136199_3_10"}
]

3 个答案:

答案 0 :(得分:1)

仅当两个属性都与给定的输入值匹配时,才会添加对象。 最后返回ArrayList对象results

for (int x = 0; x < root.getAsJsonArray().size(); x++) {
  rootObj = rootArr.get(x).getAsJsonObject();
   int i=0;
  for (String attribute : attributes) {

    for (String value : values) {
      if (rootObj.get(attribute).getAsString().equals(value)) {
           i++;
      }
    }
  }
   if(i==2){
      results.add(rootObj);
    }
}
return results;

答案 1 :(得分:1)

我错过了,但我猜你正在寻找类似的东西:

public static ArrayList<JsonObject> searchAttributes(String sensor, Map<String, String> conditions)
        throws IOException {
    establishPastEventConnection(sensor);
    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(new InputStreamReader(new FileInputStream("C:\\Dev\\test.json")));
    JsonArray rootArr = root.getAsJsonArray();
    JsonObject rootObj;
    ArrayList<JsonObject> results = new ArrayList<JsonObject>();
    for (int x = 0; x < root.getAsJsonArray().size(); x++) {
        rootObj = rootArr.get(x).getAsJsonObject();
        boolean match = true;

        for (Entry<String, String> condition : conditions.entrySet()) {
            String attribute = condition.getKey();
            String value = condition.getValue();

            if (rootObj.get(attribute) == null || !rootObj.get(attribute).getAsString().equals(value)) {
                match = false;
            }

        }

        if (match) {
            results.add(rootObj);
        }

    }
    return results;
}

你可以这样称呼它:

public static void main(String[] args) throws Exception {

    Map<String, String> conditions = new HashMap<String, String>();

    conditions.put("timeStamp", "1.483384640123117E9");
    conditions.put("eventCode", "1");

    System.out.println(searchAttributes("", conditions));

}

对不起,如果我误解了你的需求。

答案 2 :(得分:0)

我假设属性列表和值列表具有相同的大小:

mainLoop:
   for (int x = 0; x < root.getAsJsonArray().size(); x++) {
     rootObj = rootArr.get(x).getAsJsonObject();
     for (int i = 0; i < attributes.size(); ++i) {
        String attr = attributes.get(i);
        String value = values.get(i);
        if (!rootObj.get(attr).getAsString().equals(value)) {
           continue mainLoop;
        }
      }
      results.add(rootObj);
   }