【问题标题】:Error in JSONObjectJSONObject 中的错误
【发布时间】:2015-10-26 08:12:16
【问题描述】:

我在 JSONObject 中遇到编译错误:

构造函数 JSONObject(String) 未定义

    BufferedReader br = new BufferedReader(new FileReader("json.json"));
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    while (line != null) {
        sb.append(line);
        line = br.readLine();
    }

    JSONObject jsonObj = new JSONObject(sb.toString());

虽然代码仅根据 JSON 文档

【问题讨论】:

  • 编译错误?添加 Try-catch 异常 :)
  • 你用的是什么包?
  • 导入 org.json.simple.JSONArray;导入 org.json.simple.JSONObject;
  • 你应该使用 import org.json.JSONException;导入 org.json.JSONObject;

标签: java json jsonobject


【解决方案1】:
    BufferedReader br;
    try {
        br = new BufferedReader(new FileReader("json.json"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
        }
        JSONObject jsonObj = new JSONObject(sb.toString());
        System.out.println(jsonObj);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

如果找不到文件,您应该处理异常。 我认为这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多