当前位置: 首页 > 知识库问答 >
问题:

JSONArray文本必须在1[字符2行1]处以“[”开头

孔砚
2023-03-14

我有一个JSON文件,我正在尝试处理,但出现以下错误:

{ "reviewerID": "A2SUAM1J3GNN3B", 
  "asin": "0000013714", 
  "reviewerName": "J. McDonald", 
  "helpful": [2, 3], 
  "reviewText": "I bought this for my husband who plays the piano. He is having a wonderful time playing these old hymns. The music is at times hard to read because we think the book was published for singing from more than playing from. Great purchase though!", 
  "overall": 5.0, 
  "summary": "Heavenly Highway Hymns", 
  "unixReviewTime": 1252800000, 
  "reviewTime": "09 13, 2009" 
}

这是我的代码,简单地说:

JSONObject ar = new JSONObject("E:\\amazonDS.json");

    for (int i = 0; i < ar.length(); i++) {
       System.out.println( "Name: " + ar.getString("reviewerName").toString() );            
    }

共有1个答案

姬昀
2023-03-14

您必须首先读取文件的内容,因为JSONArray的构造函数需要file-content,而不是file-path。

new JSONObject(new JSONTokener(new FileInputStream(new File("path"), "UTF-8")));

new JSONObject(new JSONTokener(new FileReader("path")));

更新您应该使用filereader或为FileInputStream指定字符集

 类似资料: