Processing + android + JSON file

edited January 2014 in Android Mode

Hi,

I was very happy when I was able to parse JSON arrays and objects from a url on to local variable and display required values(without having to import any library). However, it does not seem to work in Android mode. I would be really grateful if someone showed me a way to read JSON file from a url and parse it on to local variables so that I would be able to display whole/part of the required data...

Note: I am working on an android application. So I would need a solution that works on android phones/tablets and not just in Java mode.

Thanks.

Tagged:

Answers

  • "I am working on an android application"
    In this case, post in the Android category (I moved the thread), to increase visibility of your request...

  • Sorry mate! Should have done that...

  • The Android ecosystem is very picky when it comes to application security "permissions". I may be wrong... but you should try enabling the INTERNET permission for your sketch (PDE Menu > Android > Sketch Permissions) and re-run it.

  • Guys! Sweet news! I was able to read data from a JSON file successfully! :D . But now I want to be able to read values from an array inside an object which I have been failing to. :( It would be of great help if someone enlightened me on how to do so. Following is my current code which works perfectly fine for objects but not for arrays or nested arrays:

    String url = "http://somewhere.com/hakuna/matata/mumble.json";

    void setup() { size(800, 600); background(0); String[] homePage = loadStrings(url); String jsonFragment = homePage[0]; JSONObject json = JSONObject.parse(jsonFragment); String instanceID = json.getString("instanceID"); println(pageID); }

    void draw() { }

  • Sorry! forgot to format code. Code reformatted below.

                String url = "http://somewhere.com/hakuna/matata/mumble.json";
    
                void setup() { 
    size(800, 600); 
    background(0); 
    String[] homePage = loadStrings(url); 
    String jsonFragment = homePage[0]; 
    JSONObject json = JSONObject.parse(jsonFragment);
    String instanceID = json.getString("instanceID"); 
    println(pageID);
     }
    
                void draw() {
     }
    
  • Ok! Guys I figured it out! :D :D :D

    String url = "http://somewhere.com/hakuna/matata/mumble.json";
    
    void setup() {
      size(800, 600);
      background(0);
      String[] homePage = loadStrings(url);
      String jsonFragment = homePage[0];
      JSONObject json = JSONObject.parse(jsonFragment);
      String instanceID = json.getString("instanceID");
      JSONObject items = json.getJSONArray("items").getJSONObject(0);
      String pageID = items.getString("pageID");
      println(pageID);
    }
    
    void draw() {
    }
    

    ;) ! Yay! I hope this helps another soul in need.

    Cheers! and much love from Bangalore!

  • edited January 2014

    Forum seems to mess any text w/ a link format! :-S
    My idea is to split "http://" and the rest of a link! :D

    /** 
     * JSON Request (v2.0)
     * by  jagadishnallappa (2014/Jan)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/2294/processing-android-json-file
     */
    
    final String URL = "http://" + "api.ihackernews.com/page";
    final JSONObject json = JSONObject.parse(loadStrings(URL)[0]);
    
    final String instanceID = json.getString("cachedOnUTC");
    println(instanceID);
    println();
    
    final JSONObject latestItem = json.getJSONArray("items").getJSONObject(0);
    println(latestItem);
    println();
    
    latestItem.setString("points", "100");
    println(latestItem);
    
    exit();
    
Sign In or Register to comment.