0

I'm writing a Projekt with Android Studio, where I need to parse a JSON-String. But if i try to parse it with JSONObjects, I get always null, this is my code:

import org.json.JSONObject;
public class JSONTest {

    public void test() {
        JSONObject object = new JSONObject("{\"test\":\"bla\"}");
        System.out.println(object.getString("test"));
    }
}

and it returns always null. I tried many difrent ways to solve this, but it never worked. Is there any issue with Android Studio?

Dennis Rieke
  • 201
  • 4
  • 18
  • I have tried this on my project and is working ok JSONObject object = new JSONObject("{\"test\":\"bla\"}"); String s = object.getString("test"); sorry that i can't help – Stornu2 Nov 05 '15 at 15:48
  • this are my imports import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; – Stornu2 Nov 05 '15 at 15:50
  • And Android Studio always tells me to use a try / catch try { } catch (JSONException e) { } – Stornu2 Nov 05 '15 at 15:51

1 Answers1

0

The following code works like a charm in Android Studio. The snippet has to be surrounded in a try catch block.

JSONObject object = null;

    try {
        object = new JSONObject("{\"test\":\"bla\"}");
        System.out.println(object.getString("test"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

// Output: 11-09 22:21:21.586 32743-32743/in.example I/System.out: bla