用Java解码JSON文件

问题描述 投票:-2回答:2

我正在尝试使用UTF-8解码JSON格式的JSON文件,

但它没有印刷价值

  JSONParser jsonParser = new JSONParser();
  FileReader file = new FileReader("C:/Users/aab6cob/Desktop/jsonFile/170902_K0_RUCd_ML_F4974.txt.insights-json");
  BufferedReader filBufferedReader = new BufferedReader(file);

  String st;
  while((st=filBufferedReader.readLine())!=null){
    byte[] tempByte = st.getBytes("UTF-8"); 
    String tempString = new String(tempByte);
    System.out.println(tempString);
  }
java jsondecoder
2个回答
1
投票

首先,不要随身携带.json文件格式,它只不过是一个简单的文本文件。因此,您只需使用任何fileStream等读取文件并将其写入任何文本文件中。它与读取和写入计划文本文件非常相似。

但是如果你想将JSON从文件加载到JSON对象中,我建议你阅读json-simple-read-write-json-examples

祝好运。


0
投票

如果要读取具有特定编码的文件,则无法使用FileReaderFileReader使用默认编码,这取决于系统设置。您可以使用InputStreamReader的InputStreamReader(InputStream in, String charsetName)构造函数。还有你代码中的两行

byte[] tempByte = st.getBytes("UTF-8"); 
String tempString = new String(tempByte);

应该删除,因为你只是取一个字符串,将if转换为字节,然后再转换为字符串,这是没有意义的。最后,JSON文件只是一个文本文件。阅读有关java的I / O流和字符编码的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.