We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using PubNub to publish data coming from my processing script. The trouble I am having is that the publish command expects a org.json.JSONObject and I cannot for the life of me get processing to make a org.json.JSONOnject. I have the libraries installed in the sketch and the processing IDE recognizes the class, but I get the error: "Unhandled exception type JSONException". At this point I am just using a very simple example JSON builder code for org.json taken from this site: http://theoryapp.com/parse-json-in-java/ but I have tried many different combinations. Here is my current code:
import org.json.*;
void setup() {
String str = "{ \"name\": \"Alice\", \"age\": 20 }";
org.json.JSONObject obj = new org.json.JSONObject(str);
String n = obj.getString("name");
int a = obj.getInt("age");
println(n + " " + a);
}
void draw() {
}
I am wondering if anybody has used org.json.JSONObject successfully or if there is someway to make a processing JSON object recognizable by others as a viable org.json.JSONObject.
Answers
According to JSONObject's source code:
https://GitHub.com/stleary/JSON-java/blob/master/JSONObject.java#L311
It
throws
a JSONException in that particular constructor:https://GitHub.com/stleary/JSON-java/blob/master/JSONException.java
Though it's an unchecked RuntimeException, it seems your String is somehow failing! :-SS
Although it's not gonna make the instantiation to work, adding
try/catch ()
block won't make things any worse than it already is: 8-XHey wow! That was a simple fix. It is working!!
Now, can you explain what happened in a way that my simpleton brain can understand? I don't understand what is really going on. Here are some more pointed questions:
What was processing really complaining about? Was it checking that there might be an unhandled exception? Or was the library somehow checking that there were no unhandled exceptions down the line? I ask because I had no idea how to handle that error. For example, I can write plenty of scripts that have the opportunity to have unhandled exceptions. For example if I was to just ask for a index in an array that did not exist. But the IDE would never complain about that. This seems more involved and I am confused as to who is checking and what they are checking for.
Why are processing based JSONs and org.json based JSONs not the same. Isn't the entire point of having a standard that anybody can recognize it? Why would a call require a certain library to be used to make a JSON instead of just looking for a JSON.
Thanks again for your help and explanation (and over the top emojis). Any insights might hopefully make me less reliant on this forum (although I doubt that is possible).
DISCLAIMER: I don't have that JSON library. I can'r run my own version! That was a shot in the dark. :>
extends
RuntimeException.try/catch ()
shoulda been optional for JSONObject. I don't get it either! :-/https://GitHub.com/stleary/JSON-java/blob/master/JSONObject.java
https://Processing.org/reference/JSONObject.html
package
name 1 belongs to in order to distinguish them. :-&Further explanations now...
package
java.lang! :(This is all really helpful in getting me up to speed. Processing is the only java I have ever really done. Thanks for all your help!