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

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

有效地從 JSON 文件中刪除無(wú)效字符?

有效地從 JSON 文件中刪除無(wú)效字符?

Go
Cats萌萌 2023-03-21 16:58:21
我正在通過(guò)命令行讀取文件。由于該文件是從 Oracle 導(dǎo)出的 JSON,因此它具有一定的結(jié)構(gòu)。由于某種原因,此默認(rèn)結(jié)構(gòu)不是有效的 JSON。例子:// This isn't valid JSON,"items":[{"id":123,"language":"ja-JP","location":"Osaka"},{"id":33,"language":"ja-JP","location":"Tokyo"},{"id":22,"language":"ja-JP","location":"Kentok"}]}我希望它只是一個(gè)對(duì)象數(shù)組,因此具有預(yù)期的輸出:// This is valid json[{"id":123,"language":"ja-JP","location":"Osaka"},{"id":33,"language":"ja-JP","location":"Tokyo"},{"id":22,"language":"ja-JP","location":"Kentok"}]因此,我需要?jiǎng)h除第 1 行(完全)以及}文件最后一行的最后一行。該文件正在通過(guò)輸入的命令行進(jìn)行解析:file, err := ioutil.ReadFile(os.Args[1])我試圖以這種方式刪除無(wú)效的字符串/單詞,但它不會(huì)重新格式化任何內(nèi)容:// in func main()removeInvalidJSON(file, os.Args[1])// later on .. func removeInvalidJSON(file []byte, path string) {    info, _ := os.Stat(path)    mode := info.Mode()    array := strings.Split(string(file), "\n")    fmt.Println(array)    //If we have the clunky items array which is invalid JSON, remove the first line    if strings.Contains(array[0], "items") {        fmt.Println("Removing items")        array = append(array[:1], array[1+1:]...)    }    // Finds the last index of the array    lastIndex := array[len(array)-1]    // If we have the "}" in the last line, remove it as this is invalid JSON    if strings.Contains(lastIndex, "}") {        fmt.Println("Removing }")        strings.Trim(lastIndex, "}")    }    // Nothing changed?    fmt.Println(array)    ioutil.WriteFile(path, []byte(strings.Join(array, "\n")), mode)}上面的函數(shù)確實(shí)寫入了我能看到的文件——但據(jù)我所知,它并沒(méi)有改變數(shù)組,也沒(méi)有將它寫入文件。如何有效地遠(yuǎn)程文件的第一行,以及}文件中的最后一個(gè)假花括號(hào)?我在另一個(gè)函數(shù)中解組 JSON:是否有一種方法可以使用該"encoding/json"庫(kù)更“干凈地”完成它?
查看完整描述

1 回答

?
德瑪西亞99

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

此代碼存在幾個(gè)重大問(wèn)題,導(dǎo)致其行為不符合預(yù)期。我在下面的評(píng)論中注意到了這些:


func removeInvalidJSON(file []byte, path string) {


    info, _ := os.Stat(path)

    mode := info.Mode()


    array := strings.Split(string(file), "\n")

    fmt.Println(array)


    //If we have the clunky items array which is invalid JSON, remove the first line

    if strings.Contains(array[0], "items") {

        fmt.Println("Removing items")

        // If you just want to remove the first item, this should be array = array[1:].

        // As written, this appends the rest of the array to the first item, i.e. nothing.

        array = append(array[:1], array[1+1:]...)

    }


    // Finds the last ~index~ *line* of the array

    lastIndex := array[len(array)-1]


    // If we have the "}" in the last line, remove it as this is invalid JSON

    if strings.Contains(lastIndex, "}") {

        fmt.Println("Removing }")

        // Strings are immutable. `strings.Trim` does nothing if you discard the return value

        strings.Trim(lastIndex, "}")

        // After the trim, if you want this to have any effect, you need to put it back in `array`.

    }


    // Nothing changed?

    fmt.Println(array)


    ioutil.WriteFile(path, []byte(strings.Join(array, "\n")), mode)

}

我認(rèn)為您想要的更像是:


func removeInvalidJSON(file []byte, path string) {

    info, _ := os.Stat(path)

    mode := info.Mode()


    array := strings.Split(string(file), "\n")

    fmt.Println(array)


    //If we have the clunky items array which is invalid JSON, remove the first line

    if strings.Contains(array[0], "items") {

        fmt.Println("Removing items")

        array = array[1:]

    }


    // Finds the last line of the array

    lastLine := array[len(array)-1]


    array[len(array)-1] = strings.Trim(lastLine, "}")


    fmt.Println(array)


    ioutil.WriteFile(path, []byte(strings.Join(array, "\n")), mode)

}


查看完整回答
反對(duì) 回復(fù) 2023-03-21
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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