How can I print a json string? - java

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

Related

Can't parse Java JSON string

From my server is get a JSON response like this:
String json = getJsonFromServer();
The server returns this reply (with the double quotes in the beginning and end):
"{\"error\":\"Not all required fields have been filled out\"}";
I then want to get the error field. I have tried to use the JSONObject class to get the string, but it does not work.
System.out.println("The error is: " + new JSONObject().getJSONObject(response).getString("error");
I have try
String response = "{\"error\":\"Not all required fields have been filled out\"}";
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(response);
System.out.println(json.get("error").toString());
} catch (ParseException ex) {
}
It have working, you can try it and don't forget add json lib json-simple
You seem to be using the wrong JSONObject constructor; in your example, you are trying to fetch an object from a newlay created object - it will never exist.
Try this:
String response = "{\"error\":\"Not all required fields have been filled out\"}";
JSONObject json = new JSONObject( response );
String error = json.getString( "error" );
System.out.println( error );
Yields
Not all required fields have been filled out
edit
Now that you have completely changed the question...
Why not just first strip the first and last char?
So if
String response = "\"{\"error\":\"Not all required fields have been filled out\"}\"";
response = response.substring(1, response.length() - 1));
// Now convert to JSONObject
Your response object has exactly this string?
{"error":"Not all required fields have been filled out"}
The code below printed the expected output:
Object responseObject = "{\"error\":\"Not all required fields have been filled out\"}";
JSONObject jsonObject = new JSONObject(responseObject.toString());
String errorContent = jsonObject.getString("error");
System.out.println(errorContent);

I can't read my JSON in my application

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.

Android: Exception when converting a String into a JSONObject

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.

Android - How to extract nested value from JSON string

This is my String gathered from a JSON file. It is as expected:
{
"catalog":{
"book":{
"id":"bk101",
"author":"Gambardella, Matthew",
"title":"XML Developer's Guide",
"genre":"Computer",
"price":"44.95",
"publish_date":"2000-10-01",
"description":"An in-depth look at creating applications with XML."
}
}
}
Using this String I created a JSONObject..
JSONObject jsonBook = new JSONObject(sb.toString());
Then I am simply trying to extract some of the parameters seen in the string such as id and author.
"id": "bk101"` `"author": "Gambardella, Matthew"
This is my approach..
book.setAuthor(jsonBook.getString(Book.AUTHOR));
book.setId(jsonBook.getString(Book.ID));
Yet I keep getting errors saying there is no value for id/author whichever one is first. Any ideas?
Cheers
That's because id and author are both inside book, not inside the json root
JSONObject jsonBook = new JSONObject(sb.toString());
JSONObject catalogue = jsonBook.getJSONObject("catalog");
JSONObject jbook = catalogue.getJSONObject("book");
book.setAuthor(jbook.getString(Book.AUTHOR));
book.setId(jbook.getString(Book.ID));

java nested 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...

Categories

Resources