如何在 gwt 中使用 clientbundle 使用 JSON 文件

How to use JSON file using clientbundle in gwt

我有一个静态 JSON 文件,我想在我的 GWT 代码中访问它。 custom-report.json文件在项目的client包中。我将其添加到 ClientBundle 中并尝试访问它,但出现错误

public interface AppBundle extends ClientBundle {

    @Source("custom-report.json")
    public TextResource jsonData();
    public static final AppBundle INSTANCE = GWT.create(AppBundle.class);

}

要在我的代码中使用它,这就是我正在做的事情:

AppBundle.INSTANCE.mystyle().ensureInjected();

JSONObject obj = (JSONObject) parser.parse(new FileReader(AppBundle.INSTANCE.jsonData().getText()));

这给我编译错误

[ERROR] Line 29: No source code is available for type org.json.simple.parser.JSONParser; did you forget to inherit a required module?

我不确定这是否是在 GWT 中使用 JSON 文件的正确方法。

您需要使用 GWT 提供的 com.google.gwt.json.client.JSONParser 而不是 org.json.simple.parser.JSONParser

JSONValue value = JSONParser.parse(json);
JSONObject productsObj = value.isObject();
JSONArray productsArray = productsObj.get("products").isArray();