将 org.json.simpleJSONArray 作为 Extra 传递给 Android 中的 Intent

Passing a org.json.simpleJSONArray to an Intent in Android as an Extra

我有一个问题,我从 API 调用中收到 JSONArray(来自 org.json.simple.JSONArray 库),我必须将此 JSONArray 传递给新的 page/intent 试图在我的 Android 应用程序中传输信息。

从 API 调用接收信息,我得到一个 JSONArray,我现在将其转换为字符串并将其作为字符串额外传递给新的 Intent

另一方面,当我收到额外的字符串时,我无法将它转换回 JSONArray。调用 new JSONArray(receivedString) 会导致以下错误:

JSONArray cannot be applied to java.lang.String

无论如何,我看到了这个: How to convert String to JSONObject in Java 但它对我没有帮助,因为 API 迫使我使用 json.simple 库:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

因此,我无法将我的简单 json 转换为常规 JSONArray(来自 org.json.JSONArray,因为 org.json.*org.json.simple 冲突)。

例如,如果我使用

org.json.JSONArray
org.json.JSONObect

我得到一个错误,而不是简单的库:

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

但是,如果您不需要使用 'simple',这里是上面 link 中找到的有效解决方案:

JSONObject jsonObj = new JSONObject("String");

所以我不能使用 org.json 我需要找到一种方法将包含 JSONArray 格式的字符串转换为 JSONObject 这样我就可以用使用 org.json.simple library.

如果你必须使用org.json.simple.*,这里是将java看起来像JSONArrays的字符串转换成可解析的JSONObjects

的解决方案
String searchResponseJSON = API.search(term, location);

JSONParser parser = new JSONParser();
JSONObject response = null;
try {
    response = (JSONObject) parser.parse(searchResponseJSON);
} catch (ParseException pe) {
    System.out.println("Error: could not parse JSON response:");
    System.out.println(searchResponseJSON);
    System.exit(1);
}");

我希望这对尝试使用 org.json.simple 库将字符串转换为可解析 JSONObjects 的任何人有所帮助!