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 ?

4
  • 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? Commented Oct 25, 2014 at 7:50
  • 1
    Which json library are you using, and how are you including it in your program? Commented Oct 25, 2014 at 7:57
  • I am using eclipse and I am using Java 1.7 and the library is org.json Commented Oct 25, 2014 at 7: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 ! Commented Oct 25, 2014 at 8:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 !
-4

You need to put property names and strings between quotes.

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

4 Comments

and the string values as well.
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 to get valid JSON you need to. The used parser may accept some invalid JSON.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.