第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在JAVA中獲取json文件

在JAVA中獲取json文件

江戶(hù)川亂折騰 2021-12-01 15:01:12
我有一個(gè) json 文件,其格式為:{    "results": [        {            "quiz": "1112??? ??? ??? ???? ??? ?????? ",            "answer": " X "        },        {            "quiz": "16?? ? ???? ???? ??? ???, ?? ?? ?? ??? ??? ",            "answer": " X "        },        {            "quiz": "1769? ????? ???? ?? ??? ",            "answer": "??"        }    ]}我想在JAVA區(qū)獲取這個(gè)文件。我有一個(gè)依賴(lài)項(xiàng)com.googlecode.json-simple(1.1.1 version),我有一個(gè)我寫(xiě)的代碼拋出異常:(public List<CheatImported> importJsonFile(String path) throws IOException, FileNotFoundException, ParseException {        JSONObject root = (JSONObject)jsonParser.parse(new FileReader(path));        JSONArray results = (JSONArray)root.get("results");        @SuppressWarnings("rawtypes")        Iterator iter = results.iterator();        List<CheatImported> resultList = new ArrayList<CheatImported>();        while(iter.hasNext()){            JSONObject item = (JSONObject)iter.next();            String question = (String)item.get("quiz");            String answer = (String)item.get("answer");            CheatImported imported = new CheatImported();            imported.setQuestion(question);            imported.setAnswer(answer);            resultList.add(imported);        }        return resultList;    }ArrayList 的類(lèi)型是一個(gè)只有兩個(gè)字符串屬性的類(lèi):@Getter@Setter@NoArgsConstructorpublic class CheatImported {    private String question;    private String answer;}這是我的junit代碼:@Test    public void cheatImported() throws Exception{        String path = "D:\\workspace_orderByDate\\20180105\\moonBladeQuiz\\src\\main\\resources\\static\\data.json";        List<CheatImported> list = importService.importJsonFile(path);        assertTrue(list.size() > 0);    }
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

在字符或標(biāo)記在位置 0 處意外的情況下,雖然 JSON 看起來(lái)有效,但幾乎總是字節(jié)順序標(biāo)記的問(wèn)題。

來(lái)自維基百科:

字節(jié)順序標(biāo)記 (BOM) 是一個(gè) Unicode 字符,即 U+FEFF BYTE ORDER MARK (BOM),它在文本流開(kāi)頭作為幻數(shù)出現(xiàn),可以向使用該文本的程序發(fā)出若干信號(hào)。

標(biāo)記的功能之一是指示多字節(jié)字符的哪個(gè)字節(jié)先出現(xiàn)。這稱(chēng)為流的字節(jié)序。對(duì)于 UTF-8,順序是一成不變的,因此 BOM 在 UTF-8 的上下文中沒(méi)有任何作用。

但是,JSON 規(guī)范不允許存在除空格或 JSON 結(jié)構(gòu)之外的任何標(biāo)記。字節(jié)順序標(biāo)記也不例外,因此字節(jié)順序標(biāo)記不是有效的 JSON。

您需要從文件中刪除字節(jié)順序標(biāo)記才能使其工作。

  • 在 Notepad++ 中,打開(kāi)文件并選擇Encoding ? UTF-8 (without BOM)。

  • 或者,您可以使用十六進(jìn)制編輯器打開(kāi)文件并刪除前三個(gè)字節(jié),即0xEF BB BF.


查看完整回答
反對(duì) 回復(fù) 2021-12-01
  • 1 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)