2016-08-04 4 views
0

ich eine JSON-Array haben, wie unten:Um die Anzahl der Schlüssel-Wert-Vorkommen für JSON in Java zu bekommen

[ 
    { 
    "_id": { 
     "$oid": "57833bf8cb3099a383e8e2af" 
    }, 
    "Name": "3GBWS", 
    "Version": "QV3.2", 
    "Type": "FQGA", 
    "SerialNO": "L1D73708884", 
    "Location": "TEXAS" 
    }, 
    { 
    "_id": { 
     "$oid": "5784818bcb30b4918964b50f" 
    }, 
    "Name": "3GBTW", 
    "Version": "WN6.0", 
    "Type": "FQGW", 
    "SerialNO": "O1143734584", 
    "Location": "OHIO" 
    }, 
    { 
    "_id": { 
     "$oid": "5784818bcb30b4918964b50f" 
    }, 
    "Name": "TEXAS", 
    "Version": "AS1.0", 
    "Type": "FWQA", 
    "SerialNO": "DH783708884", 
    "Location": "NY" 
    }, 
    { 
    "_id": { 
     "$oid": "5784818bcb30b4918964b50f" 
    }, 
    "Name": "3GLTS", 
    "Version": "WE9.0", 
    "Type": "FQGW", 
    "SerialNO": "L0943708884", 
    "Location": "TEXAS" 
    } 
] 

Mein Ziel wird als Texas = 2 zu erhalten.

Hier muss ich das Auftreten von "TEXAS" nur unter dem Schlüssel "Ort" bekommen. Gibt es eine Möglichkeit, das Schlüssel-Wert-Paar in Java zu vergleichen? Ich habe versucht mit int location = Collections.frequency(json_string, "TEXAS");, aber dies wird nicht den Schlüssel "Location". Bitte helfen.

+0

Verwenden Sie eine JSON-Bibliothek? – Flown

+0

@Flown: Ich habe Ihre Abfrage nicht erhalten .. Ich hole die JSON-Daten von mongodb – Alby

+0

Wie parsen Sie Ihre JSON-Zeichenfolge? – Flown

Antwort

0

Da Sie Ihre JSON-Bibliothek nicht angegeben haben, gehe ich von org.json aus.

import org.json.JSONArray; 
import org.json.JSONObject; 

public class Test { 

    public static void main(String... args) { 
    String json = "[\r\n" + 
     " {\r\n" + 
     " \"_id\": {\r\n" + 
     "  \"$oid\": \"57833bf8cb3099a383e8e2af\"\r\n" + 
     " },\r\n" + 
     " \"Name\": \"3GBWS\",\r\n" + 
     " \"Version\": \"QV3.2\",\r\n" + 
     " \"Type\": \"FQGA\",\r\n" + 
     " \"SerialNO\": \"L1D73708884\",\r\n" + 
     " \"Location\": \"TEXAS\"\r\n" + 
     " },\r\n" + 
     " {\r\n" + 
     " \"_id\": {\r\n" + 
     "  \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" + 
     " },\r\n" + 
     " \"Name\": \"3GBTW\",\r\n" + 
     " \"Version\": \"WN6.0\",\r\n" + 
     " \"Type\": \"FQGW\",\r\n" + 
     " \"SerialNO\": \"O1143734584\",\r\n" + 
     " \"Location\": \"OHIO\"\r\n" + 
     " },\r\n" + 
     " {\r\n" + 
     " \"_id\": {\r\n" + 
     "  \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" + 
     " },\r\n" + 
     " \"Name\": \"TEXAS\",\r\n" + 
     " \"Version\": \"AS1.0\",\r\n" + 
     " \"Type\": \"FWQA\",\r\n" + 
     " \"SerialNO\": \"DH783708884\",\r\n" + 
     " \"Location\": \"NY\"\r\n" + 
     " },\r\n" + 
     " {\r\n" + 
     " \"_id\": {\r\n" + 
     "  \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" + 
     " },\r\n" + 
     " \"Name\": \"3GLTS\",\r\n" + 
     " \"Version\": \"WE9.0\",\r\n" + 
     " \"Type\": \"FQGW\",\r\n" + 
     " \"SerialNO\": \"L0943708884\",\r\n" + 
     " \"Location\": \"TEXAS\"\r\n" + 
     " }\r\n" + 
     "]"; 
    JSONArray array = new JSONArray(json); 
    int count = 0; 
    for(int i = 0; i < array.length(); i++) { 
     JSONObject element = array.getJSONObject(i); 
     String location = element.getString("Location"); 
     if(location.equals("TEXAS")) { 
     count ++; 
     } 
    } 
    System.out.println("TEXAS=" + count); 
    } 
} 
+0

Das funktionierte für meinen Code..Jetzt ist es das gesamte Schlüssel-Wert-Paar..Danke für Ihre Hilfe @Flown – Alby

0

Ich würde Ihnen empfehlen, Ihre POJO-Klassen mit einem Online-Tool (z. B. http://www.jsonschema2pojo.org/) zu generieren und dann können Sie sie einfach beliebig manipulieren. Zum Beispiel mit einer statischen Methode:

public static int countLocationOccurrences(List<YourClass> collections, String location) { 
    int count = 0; 
    for(YourClass item : collections) { 
     if (item.getLocation().equals(location)) { 
      count++; 
     } 
    } 
    return count; 
} 
0

erste Filter einige Werte:

locations = j.map(elem => elem.Location) // j is the array 

danach haben Sie auch ein Array mit den Standorten, und Sie können damit machen, wie Sie möchten.

Verwandte Themen