3

There are a lot of example in Stackoverflow to show howto parse JSON. But no example worked for me and I guess I have a general issue, which I do not understand:

I get an error at following statement:

JSONObject obj2 = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");

Exception:

Exception in thread "main" java.lang.RuntimeException: Stub!
    at org.json.JSONObject.<init>(JSONObject.java:7)
    at JSONTest.main(JSONTest.java:44)

I am using Java 1.7 and the library org.json

I tried examples like: example 1 example 2

What could be the reason ?

Community
  • 1
  • 1
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • 1
    To begin with, this is not valid JSON. But the exception you get seems to indicate another problem. Which JSON library do you use? – Henry Oct 25 '14 at 07:50
  • 1
    Which json library are you using, and how are you including it in your program? – azurefrog Oct 25 '14 at 07:57
  • I am using eclipse and I am using Java 1.7 and the library is org.json – mcfly soft Oct 25 '14 at 07:58
  • 2
    OK . I guess I see the problem. Thanks for asking how I did incluse the library. It is a Android library which seems to have also JSON in it. When F3 on the JSON Object it shows me android.jar. Thats probably the reason ! – mcfly soft Oct 25 '14 at 08:01

2 Answers2

7

The problem is that you have android.jar in your project's classpath. If you are writing an Android app, you must run it in an emulator or on an actual device. (This also means you will need an Activity instead of main() and also learn the basic building blocks of an Android app.) Otherwise, you should remove anything related to Android from your project's classpath and use the standard Java SDK instead. The easiest way to clean things up is to create a brand-new Java (non-Android) project.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Yes that is it. I excluded the android jar from this eclipse project. Downloaded the JSON library and added to the project and it works. Thanks to all of you and happy weekend ! – mcfly soft Oct 25 '14 at 08:08
-4

You need to put property names and strings between quotes.

"{\"interests\": [{\"interestKey\":\"Dogs\"}, {\"interestKey\":\"Cats\"}]}"

M. Page
  • 2,694
  • 2
  • 20
  • 35
  • 1
    and the string values as well. – Henry Oct 25 '14 at 07:52
  • You don't, actually. The parser is quite lenient. The OP's code runs fine in my eclipse, as-is. The line of code they have is taken from the SO article they linked to. – azurefrog Oct 25 '14 at 07:52
  • @azurefrog to get valid JSON you need to. The used parser may accept some invalid JSON. – Henry Oct 25 '14 at 07:54
  • Which is my point. The OP is using the org.json parser, which will accept the input string they are giving. Their error has nothing to do with quotes. – azurefrog Oct 25 '14 at 07:55