La méthode getJSONObject(Chaîne de caractères) n'est pas défini pour le type JSONObject
Je suis de retour d'un json
de ma classe:
@POST("/test")
@PermitAll
public JSONObject test(Map form) {
JSONObject json=new JSONObject();
json.put("key1",1);
json.put("key2",2);
return json;
}
maintenant je veux obtenir ce json de "getInputStream
" et de l'analyser pour voir si key1
existe:
String output = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder output = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
output=output.toString();
JSONObject jsonObj = new JSONObject();
jsonObj.put("output", output);
if (jsonObj.get("output") != null){
**//search for key1 in output**
System.out.println("key1 exists");
}else{
System.out.println("key1 doesnt exist");
}
reader.close();
Comment puis-je convertir output
à JSONObject
et de la recherche pour "key1"?
J'ai essayé de suivre mais j'ai eu des erreurs après flèches:
JSONObject jObject = new JSONObject(output); ---> The constructor JSONObject(String) is undefined
JSONObject data = jObject.getJSONObject("data"); ---> The method getJSONObject(String) is undefined for the type JSONObject
String projectname = data.getString("name"); ----> The method getString(String) is undefined for the type JSONObject
- Ce que la bibliothèque ne JSONObject viennent? Vous devriez avoir accès à l'API et de trouver un moyen de les convertir à la chaîne à un JSONObject objet.
- Quel est le forfait de votre classe ? Il y a un constructeur si vous utilisez org.json.JSONObject: json.org/javadoc/org/json/JSONObject.html
- Je suis à l'aide d'json-simple-1.1.1.jar.
- Afin de lire la documentation de bibliothèque.. ou d'un commutateur à l'aide de la "attendu" librairie JSON pour le code fourni.
- Oui, essayez ces exemples: code.google.com/p/json-simple/wiki/DecodingExamples
Vous devez vous connecter pour publier un commentaire.
De l'essayer.
Et puis vous pouvez vérifier l'existence du champ à l'aide d':
Vous avez besoin pour analyser l'objet à l'aide d'un analyseur. Consultez la documentation ici: https://code.google.com/p/json-simple/wiki/DecodingExamples