简体   繁体   中英

JSON parsing throws error

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 ?

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.

您需要将属性名称和字符串放在引号之间。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM