This question has been asked earlier, but I am unable to figure out the error in my code from the responses to those questions.
I am trying to convert a java string into json object. Here is the code:
import org.json.JSONObject;
//Other lines of code
URL seatURL = new URL("http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2");
//Return the JSON Response from the API
BufferedReader br = new BufferedReader(new InputStreamReader(seatURL.openStream(),Charset.forName("UTF-8")));
String readAPIResponse = " ";
StringBuilder jsonString = new StringBuilder();
while((readAPIResponse = br.readLine()) != null){
jsonString.append(readAPIResponse);
}
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonString);
System.out.println("---------------------------");
System.out.println(jsonObj);
The output is:
{"title":"Free Music Archive - Genres","message":"","errors":[],"total":"163","total_pages":82,"page":1,"limit":"2","dataset":[{"genre_id":"1","genre_parent_id":"38","genre_title":"Avant-Garde","genre_handle":"Avant-Garde","genre_color":"#006666"},{"genre_id":"2","genre_parent_id":null,"genre_title":"International","genre_handle":"International","genre_color":"#CC3300"}]}
---------------------------
{}
So, as you can see, the jsonstring is getting the data, but the jsonObj does not. I am using org.json JAR.
The string that you pass to the constructor
JSONObject
has to be escaped withquote()
:Your code would now be:
@Nishit, JSONObject does not natively understand how to parse through a StringBuilder; instead you appear to be using the JSONObject(java.lang.Object bean) constructor to create the JSONObject, however passing it a StringBuilder.
See this link for more information on that particular constructor.
http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.Object%29
When a constructor calls for a java.lang.Object class, more than likely it's really telling you that you're expected to create your own class (since all Classes ultimately extend java.lang.Object) and that it will interface with that class in a specific way, albeit normally it will call for an interface instead (hence the name) OR it can accept any class and interface with it "abstractly" such as calling .toString() on it. Bottom line, you typically can't just pass it any class and expect it to work.
At any rate, this particular constructor is explained as such:
So, what this means is that it's expecting you to create your own class that implements get or is methods (i.e.
or
So, to solve your problem, if you really want that higher level of control and want to do some manipulation (e.g. modify some values, etc.) but still use StringBuilder to dynamically generate the code, you can create a class that extends the StringBuilder class so that you can use the append feature, but implement get/is methods to allow JSONObject to pull the data out of it, however this is likely not what you want/need and depending on the JSON, you might spend a lot of time and energy creating the private fields and get/is methods (or use an IDE to do it for you) or it might be all for naught if you don't necessarily know the breakdown of the JSON string.
So, you can very simply call
toString()
on the StringBuilder which will provide a String representation of the StringBuilder instance and passing that to the JSONObject constructor, such as below:You are passing into the
JSONObject
constructor an instance of aStringBuilder
class.This is using the
JSONObject(Object)
constructor, not theJSONObject(String)
one.Your code should be:
Your json -
Using the JSON library from
json.org
-NOTE:
The following information will be helpful to you - json.org.
UPDATE:
Converting the String to JsonNode using ObjectMapper object :