Actually i want to make a MCQ for a medical application in Android ! So i want to get question and my possible choice from my database but i have a problem when i try to get my question witch him choice. My error is showed by the JSONException and i don't know why :(
I check my JSON with jsonlint.com so i think it's ok for that.
Here is my JSON :
{
"QCM": {
"1": {
"question": "Est-ce que Captain America gagne contre IronMan",
"id": "31"
},
"2": {
"choix": "Oui"
},
"3": {
"choix": "Non"
}
}
}
and here is my JAVA from my Android application.
try {
JSONObject lesQuestions = response.getJSONObject("QCM");
Iterator<?> keys = lesQuestions.keys();
while(keys.hasNext()) {
String key = (String) keys.next();
if (lesQuestions.get(key) instanceof JSONObject) {
JSONObject obj = (JSONObject) lesQuestions.get(key);
String signesCliniques = obj.getString("question");
String choix = obj.getString("choix");
lesChoixButton.setText(choix);
symptomesQuestions.setText(signesCliniques);
}
}
}
i hope you can help me !
Not every JSONObject in your JSON data has "question" and "choix" key. I am quite sure that you are getting org.json.JSONException: No value for ......
Make sure to check if there is "question" or "choix" parameter before you attempt to access it.
EDIT:
To check, you can use JSONObject.has(String parameter) or JSONObject.isNull(String parameter) method. Link: http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
I suggest changing the structure of your JSON. Having a JSONObject for the question and a JSONArray for the Choices. Still up to you though if you still want to proceed with your current implementation. What you could try to use is JSON.optString(). It would return a value depending if the key exists, else it will return an empty string. As per its description:
Returns the value mapped by name if it exists, coercing it if necessary, or the empty string if no such mapping exists.
Use it like so, change your:
String choix = obj.getString("choix");
to
String choix = obj.optString("choix");
It should work. You can make it better though.
The Correct answer is, first when you get your response from your server, parse/convert it into JSONObject like
JSONObject rootObject = new JSONObject (response.toString());
and Then follow your JSON parsing as you are doing. This will do for sure.
JSONObject lesQuestions = rootObject.getJSONObject("QCM");
Android JSON parsers provide another optional method which begins with "opt" in comparison to methods which begin with "get". The significance with opt is that, it will return a NULL result and not throw an exception.
E.g:
obj.getString("question"); will throw an exception if the JSON does not have a key with value "question."
However,
String s = obj.optString("question"); will NOT throw an exception if the JSON does not have a key with value "question". Your result string will simply be NULL.
I find the opt methods very handy rather than the get methods which throws exceptions.
Related
I have an object something like this in my database and now my requirement is to find the value of particular field such as name and if present return true,
{
"_id" : "123",
"name" : "Team"
}
but in some case the field name itself doesn't exist. Sample can be something like this:
{
"id":1234
}
In this case I need to return false.
How can I validate if name field exist in particular object?
I was trying to use StringUtils method something like this
StringUtils.isBlank(obj.getName); But its throwing It is throwing java.lang.NullPointerException .
You can use Json schema validator. If your json will be in specific format. Please have a look at Jackson library.
JSONObject class has a method named "has". try this way,
if (json.has("name")) {
String status = json.getString("name"));
}
This will work
You can use Gson Java library to serialize and deserialize Java objects to JSON (as given below).
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(object, JsonObject.class);
Then, you can use the has method in JsonObject, to check if the key exists in it.
jsonObject.has(key)
Example:
Below is a method to check if given key exists in given json string, and get it's value.
(Instead of String, you can use your object as well. Here, I am considering the jsonStr as a String object, for better understanding.)
private String getValueFromJsonForGivenKey(String key, String jsonStr) {
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonStr, JsonObject.class);
if (jsonObject.has(key)) {
// The given JSON string has the given key
String value = jsonObject.get(key).getAsString();
return value;
}
return null;
}
For key id and for jsonStr { "id": "1234" }, we get 1234.
For key name and for jsonStr { "id": "1234" }, we get null.
What you can do is to use JSONObject's opt method
eg.
JSONObject jsonObject = new JSONObject(myJSONString);
String name = jsonObject.optString("name");
I'm doing a school project (newbie alert) where I need to use the zomato's website to get a cuisine's ID and name depending on the ID I send. This is the code in the demo file:
WebServiceConnection web = new WebServiceConnection("xxxxxxxxxxxx");
AcessoDados acesso = new AcessoDados("https://api.zomato.com/v1/cuisines.json", web);
String aces = acesso.getCuisines(310);
JSONObject obj = new JSONObject();
System.out.println(obj.get(aces));
All the methods called here were made by our teacher.
WebServiceConnection and AcessoDados function get the link to Zomato's API and the user key. (Zomato API requires a key to work)
"acesso.getCuisines(310)" sends the the value "310", and the method "getCuisines" should return a json string with all the establishments it can find in their website (310 is the ID for Porto, Portugal).
However, it only prints "[]" in the output (without the quotes). It should print something like this:
[
{"cuisine_name":"African","cuisine_id":152},
{"cuisine_name":"American","cuisine_id":1},
{"cuisine_name":"Angolan","cuisine_id":951},
{"cuisine_name":"Cafe","cuisine_id":30},
(...)
]
I can't find what the problem is or if I'm making any obvious mistake. Am I missing something here?
If System.out.println(aces); prints [], then Zomato's API is returning an empty response, which means that 310 is not a valid id. But, if aces prints a non-empty string and is a JSON String then the following solution should work.
You are not parsing the JSON string which was received by Zomato API. Instead, you are creating a new JSONObject which is empty.
Try doing the following :
JSONObject obj = null;
try {
obj = (JSONArray) JSONValue.parseWithException(aces);
System.out.println(obj);
} catch (ParseException e) {
e.printStackTrace();
}
Assumptions :
You are using json-simple
I get the following Error when I try to convert a JSON String into a JSONObject.
Value 48.466667|9.883333 at location of type java.lang.String
cannot be converted to JSONObject
The String is valid JSON, I tested it with http://jsonlint.com/
Example:
{"name":"An der Decke","location":"48.412583|10.0385","type":"Virtual","size":null,"status":"Available","difficulty":1,"rating":null,"terrain":1}
The code that produces the exception looks like that:
jsonObject = new JSONObject(result);
jsonArray = new JSONArray();
Iterator<String> iter = jsonObject.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject value = (JSONObject) jsonObject.get(key); <---- Exception
jsonArray.put(value);
} catch (JSONException e) {
// Something went wrong!
}
}
Is the pipe | symbol not a valid character in Java JSON?
EDIT:
The thing is, it works fine if the JSON String doesn't include the "location":"48.412583|10.0385" part...
You seem to misunderstand how the org.json library works.
As explained on the JSON homepage, a JSON value can be a string, number, object, array, true/false or null. The library maps these value types to String, Number subclasses, JSONArray, JSONObject, Boolean or null.
Not everything in that library is a JSONObject. In fact, a JSONObject is specifically used to represent a name/value pair object. JSONObject.get() can potentially return any of the aforementioned value types, that's why it needs to fall back to the greatest common denominator type: Object (and not JSONObject). Thus, casting everything to a JSONObject won't work.
It's your responsibility to ensure that you're casting to the correct type using your knowledge of the incoming data structure. This seems to be a problem in your case: your JSON string contains strings (for name, location, type and status), integers (for difficulty and terrain) and nulls (for size). What exactly are you trying to do with these?
If your goal is just to get a JSONArray of all your JSON string values, there's a much simpler way to do it.
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.toJSONArray(jsonObject.names());
System.out.println(jsonArray); // prints:
// [1,"Available","48.412583|10.0385","An der Decke",1,null,"Virtual",null]
With that aside, you were wrong to assume that every value encapsulated within JSON would be a JSON object itself. In fact, in your case none of them are. The correct types of all the values in your JSON are
// String
System.out.println(jsonObject.getString("name")); // An der Decke
System.out.println(jsonObject.getString("location")); // 48.412583|10.0385
System.out.println(jsonObject.getString("type")); // Virtual
System.out.println(jsonObject.getString("status")); // Available
// Null
System.out.println(jsonObject.isNull("size")); // true
System.out.println(jsonObject.isNull("rating")); // true
// Integer
System.out.println(jsonObject.getInt("terrain")); // 1
System.out.println(jsonObject.getInt("difficulty")); // 1
On the other hand, if your name was an embedded JSON object consisting of first, middle and last names, your JSON string (ignoring the rest of the keys for brevity) would have looked like
{"name": {"fname" : "An", "mname" : "der", "lname" : "Decke"}}
Now, we can put getJSONObject() to use because we really do have an embedded JSON object.
JSONObject jsonObj = new JSONObject("{\"name\":
{\"fname\" : \"An\", \"mname\" : \"der\", \"lname\" : \"Decke\"}}");
// get embedded "name" JSONObject
JSONObject name = jsonObj.getJSONObject("name");
System.out.println(name.getString("fname") + " "
+ name.getString("mname") + " "
+ name.getString("lname")); // An der Decke
The get() method of JSONObject returns a result of type Object. In this case, it seems it is a String. It's as if you were doing
JSONObject value = (JSONObject) new String("asdasdsa");
which obviously makes no sense as they are incompatible types.
Instead, retrieve the value, create a JSONObject from it and add it to the JSONArray.
I want to know if it is possible to check if some key exists in some jsonArray using java. For example: lets say that I have this json string:
{'abc':'hello','xyz':[{'name':'Moses'}]}
let's assume that this array is stored in jsnArray from Type JSONArray.
I want to check if 'abc' key exists in the jsnArray, if it exists I should get true else I should get false (in the case of 'abc' I should get true).
Thnkas
What you posted is a JSONObject, inside which there is a JSONArray. The only array you have in this example is the array 'xyz', that contains only one element.
A JSONArray example is the following one:
{
'jArray':
[
{'hello':'world'},
{'name':'Moses'},
...
{'thisIs':'theLast'}
]
}
You can test if a JSONArray called jArray, included inside a given JSONObject (a situation similar to the example above) contains the key 'hello' with the following function:
boolean containsKey(JSONObject myJsonObject, String key) {
boolean containsHelloKey = false;
try {
JSONArray arr = myJsonObject.getJSONArray("jArray");
for(int i=0; i<arr.length(); ++i) {
if(arr.getJSONObject(i).get(key) != null) {
containsHelloKey = true;
break;
}
}
} catch (JSONException e) {}
return containsHelloKey;
}
And calling that in this way:
containsKey(myJsonObject, "hello");
Using regular expressions will not work because of the opening and closing brackets.
You could use a JSON library (like google-gson) to transform your JSON Array into a java array and then handle it.
JSON arrays don't have key value pairs, JSON objects do.
If you store it as a json object you can check the keys using this method:
http://www.json.org/javadoc/org/json/JSONObject.html#has(java.lang.String)
If you use JSON Smart Library in Java to parse JSon String -
You can parse JSon Array with following code snippet -
like -
JSONObject resultsJSONObject = (JSONObject) JSONValue.parse(<<Fetched JSon String>>);
JSONArray dataJSon = (JSONArray) resultsJSONObject.get("data");
JSONObject[] updates = dataJSon.toArray(new JSONObject[dataJSon.size()]);
for (JSONObject update : updates) {
String message_id = (String) update.get("message_id");
Integer author_id = (Integer) update.get("author_id");
Integer createdTime = (Integer) update.get("created_time");
//Do your own processing...
//Here you can check null value or not..
}
You can have more information in - https://code.google.com/p/json-smart/
Hope this help you...
I am connecting to a third party API and getting back a long JSON string. I only need one value from it, but it is located pretty deep inside the hierarchy. Is there a simple way to get it, without going through the whole thing? I looked all over but nothing seems easy.
Here's my example:
"response":{"status":1,"httpStatus":200,"data":{"myDesiredInfo":"someInfo"},"errors":[],"errorMessage":null}}
I've been trying to use Gson so I can get this blob as a JsonObject. I was sure there's something simple, like this:
jsonObject.get("myDesiredInfo")
or at the minimum something like this:
jsonObject.get("response.data.myDesiredInfo")
But it doesn't seem to exist.
So is there any parser out there that will allow me to do this?
This is my json string
String s="{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}";
I use following code to get housename
JsonElement je = new JsonParser().parse(s);
JsonObject asJsonObject = je.getAsJsonObject();
JsonElement get = asJsonObject.get("address");
System.out.println(s + "\n" + get);
JsonObject asJsonObject1 = get.getAsJsonObject();
JsonElement get1 = asJsonObject1.get("housename");
System.out.println(get1);
The Following is my output :
{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}
{"housename":"villa"}
"villa"
I don't think there is another way to do this. I also tried to do in other ways but i didn't get any output.
The following way you can retrieve from your jsonObject.
JSONObject jObject = new JSONObject(yourresponse);
Log.i("Desired Info is ",jObject.getJSONObject("response").getJSONObject("data").getString("myDesiredInfo"));
I wrote a little utility method that uses Gson's API to get a value as String from a JSON object, based on a java.util.List of values. So for my original question the list objects will be "response", "data", "myDesiredInfo."
Surely this can be improved on, but it's a start.
/*
* Takes a JsonObject and parses it for a primitive value, going level by level
* according to the values in #infos
*/
public static String parseJson(JsonObject json, List<String> infos) {
try {
if(infos.size() == 0) {
return json.toString();
}
JsonElement je = json.get((String)infos.get(0));
infos.remove(0);
if(je instanceof JsonObject) {
return parseJson(je.getAsJsonObject(), infos);
} else {
return je.getAsString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Most languages have a JSON decoding library, a lot of them native. No idea what language you're using so here's PHP as an example:
$jsonObj = json_decode($json);
$json->response->data->myDesiredInfo;
Ruby, Python, Java - all these languages have good libraries.