How do I read/load a JSON formatted String

edited March 2015 in How To...

I have a JSON formatted string that I want to load into a JSON object. I cannot read this from a file or a URL, I need to parse the string. Any help greatly appreciated.

Answers

  • Hi Chrisir, loadJSONObject() assumes you are loading the data from a file or a URL, as I said in my post I cannot use either and if you try to load a string you get the error "The function loadJSONObject(String) does not exist"

  • never mind

    doesn't work

  • edited March 2015

    here...

    they forgot it in the reference...

    but it works similar to parseXML() I think....

    ;-)

    // The following short JSON String is parsed 
    // in the code below. 
    //
    String t1 = " {   \"id\": 0,   \"species\": \"Panthera leo\",   \"name\": \"Lion\" } ";
    
    JSONObject json;
    
    void setup() {
    
      // json = new JSONObject(t1); 
      // json = loadJSONObject(t1);
    
      json = parseJSONObject(t1);
    
    
      int id = json.getInt("id");
      String species = json.getString("species");
      String name = json.getString("name");
    
      println(id + ", " + species + ", " + name);
    }
    
  • I had tried that earlier and got the error "the constructor JSONObject(Object) is not visible" I am using Processing 2.0.2, I assume you are using a later version in which it is visible?

  • it's still not trivial I guess - you need to know the format of the json-string, the names of all fields and arrays......

  • It's parseJSONObject ?

  • yeah, I am on 3.0a5

  • I have moved to procesing version 2.2.1 and used your example and get the same error "the constructor JSONObject(Object) is not visible" when the constructor is called json = new JSONObject(t1);

  • edited March 2015

    Undocumented method parseJSONObject() exists in Processing 2.2.1. No need for any alpha versions! ;)
    Although I think its implementation is slower than it should be to say the least: :O)

    public JSONObject parseJSONObject(String input) {
      return new JSONObject(new java.io.StringReader(input));
    }
    

    Why instantiate a StringReader for a mere String?
    IMO it makes more sense to directly create a JSONTokener from the String and pass it to the JSONObject: :-B

    public JSONObject parseJSONObject(String input) {
      return new JSONObject(new JSONTokener(input));
    }
    

    But then we get: "The constructor JSONObject(JSONTokener) is not visible"! X(
    Even worse, the JSONTokener class itself isn't public nor protected!!!
    Seems like the guys messing w/ the JSON stuff aren't friendly at letting their stuff public! >:P

    Most Processing's API are public or at least protected.
    However this policy isn't being implemented to JSON code and it's locking us away! :-w

  • quote

    when the constructor is called json = new JSONObject(t1);

    as said

    It's parseJSONObject !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ;-)

  • String t1 = " {   \"id\": 0,   \"species\": \"Panthera leo\",   \"name\": \"Lion\" } ";
    
    JSONObject json;
    
    void setup() {
    
      json = parseJSONObject(t1);  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
  • Thanks Chrisir, all working . Note to self, "try reading what somebody writes!"

  • that's a lesson....

    but how about that... often I make a note on paper for a thing I have to do and next day I can't read it any more... because my hand-writing is too bad...

    ;-)

Sign In or Register to comment.