processing.data.JSONObject to org.json.JSONObject conversion

edited May 2016 in Library Questions

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

  • edited May 2016

    According to JSONObject's source code:
    https://GitHub.com/stleary/JSON-java/blob/master/JSONObject.java#L311

    public JSONObject(String source) throws JSONException {
        this(new JSONTokener(source));
    }
    

    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-X

    // forum.Processing.org/two/discussion/16425/
    // processing-data-jsonobject-to-org-json-jsonobject-conversion
    
    // GoToLoop (2016-May-04)
    
    import org.json.JSONObject;
    import org.json.JSONException
    
    String str = "{ \"name\": \"Alice\", \"age\": 20 }";
    
    org.json.JSONObject json;
    try {
      json = new org.json.JSONObject(str);
    }
    catch (JSONException e) {
      System.err.println(e);
      exit();
    }
    println(json, ENTER);
    
    String name = json.getString("name");
    int age = json.getInt("age");
    println(name, age);
    
    exit();
    
  • Hey 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:

    1. 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.

    2. 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).

  • edited May 2016

    DISCLAIMER: I don't have that JSON library. I can'r run my own version! That was a shot in the dark. :>

    1. What was processing really complaining about?:
      • Java exceptions are split as checked & unchecked.
      • As mentioned, JSONException extends RuntimeException.
      • Therefore try/catch () shoulda been optional for JSONObject. I don't get it either! :-/
      • Perhaps you've got a very old version in which JSONException was a subclass of some other checked Exception? :-\"
    2. : Why are Processing-based JSONs and org.json-based JSONs not the same?:
      • They have some classes sharing the same name like JSONObject:
        https://GitHub.com/stleary/JSON-java/blob/master/JSONObject.java
        https://Processing.org/reference/JSONObject.html
      • But they have totally diff. implementations internally! :-@
      • And I bet each 1 have methods that the other doesn't have either.
      • Moreover they belong to diff. packages too.
      • In the particular case of JSONObject, b/c that class name is the same for both libraries, we have to prefix them w/ the package name 1 belongs to in order to distinguish them. :-&
      • Either org.json.JSONObject or processing.data.JSONObject.
  • edited May 2016 Answer ✓

    The trouble I am having is that the publish command expects a org.json.JSONObject

    Why would a call require a certain library to be used to make a JSON instead of just looking for a JSON.

    Further explanations now...

    • Java is very strict about datatypes as we all know.
    • If something requires a String object, we can create our own class also called String.
    • And having all methods that the original String got as well.
    • Regardless, it's gonna be refuted and won't compile b/c it's not the original String which belongs to 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!

Sign In or Register to comment.