2016-08-19 4 views
0

ich eine JSON habe wie:Wie der JSON-Wert unter der Bedingung holen

{ 
    "data":{ 
     "mapping":[ 
     { 
      "erp":"SYMIX", 
      "plant":"RY1", 
      "region":"NA" 
     }, 
     { 
      "erp":"SAP", 
      "plant":"EXM", 
      "region":"ASIA" 
     } 
     ] 
    } 
} 

bekomme ich einen Wert von Pflanze auf dieser Grundlage ich den Wert von erp erhalten möge. wie wenn ich RY1 bekomme, muss SYMIX abgeholt werden. Wie kann ich dies mit Java erreichen

+1

Welche json-Bibliothek verwenden Sie? –

+0

Sie müssen eine Pojo-Klasse für die Zuordnung erstellen und dann eine Liste dieser Klasse mit JSON-Werten erstellen, dann können Sie leicht Objekt aus der Liste – Vickyexpert

+0

@RakeshGR Ich benutze org.JSON –

Antwort

1

Es ist leicht mit jayway Bibliothek getan werden könnte:

<dependency> 
     <groupId>com.jayway.jsonpath</groupId> 
     <artifactId>json-path</artifactId> 
    </dependency> 

Das Beispiel:

import java.util.List; 

import com.jayway.jsonpath.JsonPath; 

public class TestJson { 
private static final String JSON = "{\"data\":{\"mapping\":[{\"erp\":\"SYMIX\",\"plant\":\"RY1\",\"region\":\"NA\"},{\"erp\":\"SAP\",\"plant\":\"EXM\",\"region\":\"ASIA\"}]}}"; 

public static void main(String[] args) { 
    String erp = getErp("RY1"); 
    System.out.println(erp); 
} 

private static String getErp(String plant) { 
    List<String> values = JsonPath.read(JSON, String.format("$.data.mapping.[?(@.plant==%s)].erp", plant)); 
    return values.isEmpty() ? null : values.get(0); 
} 
} 

Die Ausgabe lautet: SYMIX


AKTUALISIERT:

Ok, hier Version von Datei zu lesen:

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.List; 

import com.jayway.jsonpath.JsonPath; 

public class TestJson { 
    public static void main(String[] args) throws Exception { 
     String pathToFile = "file.json"; 
     String plant = "RY1"; 

     File file = openFile(pathToFile); 
     String erp = readErpFromFile(file, plant); 
     System.out.println("Erp: " + erp); 
    } 

    private static String readErpFromFile(File file, String plant) throws Exception { 
     List<String> values = JsonPath.read(file, String.format("$.data.mapping.[?(@.plant==%s)].erp", plant)); 
     return values.isEmpty() ? null : values.get(0); 
    } 

    private static File openFile(String strPath) throws IOException { 
     Path path = Paths.get(strPath); 
     System.out.println("Trying to read file: " + path.toAbsolutePath().toString()); 
     return path.toFile(); 
    } 
} 

Ersetzen Sie einfach "file.json" Zeichenfolge Pfad der Datei.

+0

Es funktioniert gut. . .Mine JSON ist in einer Datei, also wie kann ich es in String Variable konvertieren –

0

Okay ... Ich habe mich für einen ziemlich direkten Ansatz entschieden. Try this:

public static Object searchArrayofObjects(JSONArray array, String searchByKey, String resultKey, Object searchObject) throws JSONException 
{ 
    Object result = null; 
    for (int i = 0; i < array.length(); i++) 
    { 
     JSONObject innerObject = array.getJSONObject(i); 
     if (innerObject.has(searchByKey) && innerObject.get(searchByKey).equals(searchObject)) 
     { 
      result = innerObject.has(resultKey) ? innerObject.get(resultKey) : null; 
      break; 
     } 
    } 
    return result; 
} 

Jetzt searchArrayofObjects nennen (Mapping, "plant", "erp", "RY1") und es sollte "Symix" zurückzukehren.
Ich habe es offensichtlich ein wenig generisch gemacht. Aber das ist der allgemeine Ansatz, den ich denken sollte.

Verwandte Themen