У меня есть следующая JSON строка:
private String jsonString = "{\"response\":{\"status\":\"ok\",\"userTier\":\"developer\",\"total\":12,\"startIndex\":1,\"pageSize\":10,\"currentPage\":1,\"pages\":2,\"results\":[{\"id\":\"technology/apple\",\"type\":\"keyword\",\"sectionId\":\"technology\",\"sectionName\":\"Technology\",\"webTitle\":\"Apple\",\"webUrl\":\"https://www.theguardian.com/technology/apple\",\"apiUrl\":\"https://content.guardianapis.com/technology/apple\",\"references\":[{\"id\":\"reuters-stock-ric/AAPL.O\",\"type\":\"reuters-stock-ric\"}]}]}}";
, и в ней есть массив JSON под названием «результаты» "results":[{"id":"technology/apple","type":"keyword"
...
Я использую следующий код чтобы извлечь массив и затем получить из него данные:
try {
// build up a list of News objects with the corresponding data.
JSONObject jsonRootObject = new JSONObject(newsJSON);
// Extract the JSONArray called "results",
// which represents a list of data for every news article
JSONArray resultsArray = jsonRootObject.optJSONArray("results");
Однако android Studio выдает следующее сообщение об ошибке:
E/MainActivity: Problem parsing the News JSON results
org.json.JSONException: No value for results
at org.json.JSONObject.get(JSONObject.java:400)
at org.json.JSONObject.getJSONArray(JSONObject.java:595)
at com.example.readjson.MainActivity.extractFeatureFromJson(MainActivity.java:68)
at com.example.readjson.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7989)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3485)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2045)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7478)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)