简体   繁体   English

json-simple utf-8解析Java

[英]json-simple utf-8 parsing in Java

I'm trying to parse JSON using json-simple-1.1.1 我正在尝试使用json-simple-1.1.1解析JSON

public static void main(String[] args) throws ParseException, IOException{

    BufferedReader buff = new BufferedReader(new FileReader("src/qqqqqqqq/json"));

    String line = null;

    while((line = buff.readLine()) != null){

        JSONParser parser = new JSONParser();
        Object obj = (Object) parser.parse(line);
        JSONObject jsonObj = (JSONObject) obj;

        System.out.println((String)jsonObj.get("name"));
    }
}

My JSON source file using UTF-8 without BOM 我的JSON源文件使用UTF-8而没有BOM

{"name":"ą"}
{"name":"ć"}
{"name":"ń"}
{"name":"ź"}
{"name":"ż"}
{"name":"ó"}

Output from println: println的输出:

Ä…
ć
Ĺ„
Ĺş
ĹĽ
Ăł

What I am doing wrong? 我做错了什么?

A FileReader uses the default charset which must not be UTF-8. FileReader使用默认字符集,该字符集不能是UTF-8。

Use 采用

new BufferedReader(new InputStreamReader(new FileInputStream("src/qqqqqqqq/json"), "UTF-8"));

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM