Java reads json data and solves Chinese garbled characters
Java code that reads json data and appears garbled: (Recommended: java video tutorial)
//从json文件中读取数据 StringBuffer stringBuffer = new StringBuffer(); try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; while((line=bufferedReader.readLine()) != null) { stringBuffer.append(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //开始解析 JSONObject jsonObject = new JSONObject(stringBuffer.toString());
Solution
according to According to the online description, the file is set to UTF-8 encoding, but if it is saved as a UTF-8 encoded file, there will be 3 more bytes of data identifying the encoding type in the file header, causing the JSONObject object to fail to be parsed. The reason: it is not { beginning.
Idea: We open it by editing, and the UTF-8 encoding format is displayed normally. Then we use the binary editing tool to delete the UTF-8 identifier EF BB BF in the file header, and then in the code, use InputStreamReader to let the data flow from UTF-8 encoding is used in the process of converting byte stream to character stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Java reads json data and solves Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.