亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有效地從 JSON 文件中刪除無效字符?

有效地從 JSON 文件中刪除無效字符?

Go
Cats萌萌 2023-03-21 16:58:21
我正在通過命令行讀取文件。由于該文件是從 Oracle 導出的 JSON,因此它具有一定的結構。由于某種原因,此默認結構不是有效的 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"}]}我希望它只是一個對象數組,因此具有預期的輸出:// 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"}]因此,我需要刪除第 1 行(完全)以及}文件最后一行的最后一行。該文件正在通過輸入的命令行進行解析:file, err := ioutil.ReadFile(os.Args[1])我試圖以這種方式刪除無效的字符串/單詞,但它不會重新格式化任何內容:// 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)}上面的函數確實寫入了我能看到的文件——但據我所知,它并沒有改變數組,也沒有將它寫入文件。如何有效地遠程文件的第一行,以及}文件中的最后一個假花括號?我在另一個函數中解組 JSON:是否有一種方法可以使用該"encoding/json"庫更“干凈地”完成它?
查看完整描述

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

此代碼存在幾個重大問題,導致其行為不符合預期。我在下面的評論中注意到了這些:


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)

}

我認為您想要的更像是:


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)

}


查看完整回答
反對 回復 2023-03-21
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號