Comparaison String avec une valeur d'un JSONObject
Bonsoir tout le monde,
Je vous explique mon problème: Je crée un fichier JSON (JSONObject) à partir d'un fichier texte. A partir de ce fichier dans ma méthode getDataCountryCode(), j'aimerais récupérer les informations présentes dans la fonction set d'une donnée présente dans mon JSONObject qui correspond au paramètre ISO3166_1_Alpha_2 dans le fichier (c'est à dire comparer ma variable en paramètre avec uniquement les valeurs de ISO3166_1_Alpha_2 et retourner les informations correspondantes).
Voici la structure de mon fichier, j'ai affiché l'ArrayList
Code:
1 2
| List:{"NDC":" ","ISO3166_1_Alpha_3":"ZMB","is_independent":"Yes","CC":"260","name":"Zambia","lng":"30","ITU_E212":"645","lat":"-15","ISO3166_1_Alpha_2":"ZM"}
18788 [http-bio-8080-exec-5] INFO main.java.CountriesUtil - List:{"NDC":" ","ISO3166_1_Alpha_3":"ZWE","is_independent":"Yes","CC":"263","name":"Zimbabwe","lng":"30","ITU_E212":"648","lat":"-20","ISO3166_1_Alpha_2":"ZW"} |
Par exemple, dans mon get j'ai en variable "ZM", j'aimerais afficher : [ { "ISO3166_1_Alpha_2":"ZM", "ITU_E212":"645", "Dialing_Code":"260" } ] comme indiqué dans la fonction set. (j'aimerais comparer qu'avec les valeurs d'ISO3166_1_Alpha_2 comme ça aucune ambiguïté).
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| private static final JSONArray ja = new JSONArray();
private static JSONObject jo = new JSONObject();
private static final Hashtable<String, String> data = new Hashtable<String, String>();
private static void set(String key, JSONObject jo) {
data.put(key, "[ \n { \n \"ISO3166_1_Alpha_2\":\"" + (String) jo.get("ISO3166_1_Alpha_2") + "\","
+ "\n\"ITU_E212\":\"" + (String) jo.get("ITU_E212") + "\","
+ "\n\"Dialing_Code\":\"" + (String) jo.get("CC") + "\"\n } \n ]");
}
@SuppressWarnings("unchecked")
public static void init(ServletContext servletContext) {
try {
InputStream ips = servletContext.getResourceAsStream("/WEB-INF/countries.txt");
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String ligne;
boolean first = true;
String[] keys = null;
String[] values = null;
// create json
while ((ligne = br.readLine()) != null) {
ligne = encode(ligne);
if (first) {
first = false;
keys = ligne.split(";");
for (int i = 0; i < keys.length; i++)
logger.info(keys[i]);
} else {
values = ligne.split(";");
jo = new JSONObject();
for (int i = 0; i < keys.length; i++) {
jo.put(keys[i], values[i]);
}
ja.add(jo);
logger.info(jo.toJSONString());
}
}
nbCountries = ja.size();
for (int i = 0; i < ja.size(); i++) {
jo = (JSONObject) ja.get(i);
String CC = (String) jo.get("CC");
String NDC = (String) jo.get("NDC");
if (!NDC.isEmpty()) {
String[] NDCs = NDC.split(",");
for (int j = 0; j < NDCs.length; j++) {
String CCNDC = CC + NDCs[j];
logger.info("CCNDC: " + CCNDC);
CCNDC = CCNDC.trim();
if (CCNDC.length() > size)
size = CCNDC.length() + 1;
set(CCNDC, jo);
}
} else {
set(CC, jo);
logger.info("CCNDC: " + CC);
}
}
br.close();
} catch (Exception e) {
logger.info("Could not load country file",e);
}
}
public static String getDataCountryCode(String charac) {
for(int i = 0; i< ja.size(); i++){
logger.info("List:" + ja.get(i));
if (ja.equals(charac)) {
return data.get(charac);
}
}
return null;
} |
Merci à vous (j'espère ne pas avoir été trop répétitif :P)