有效地从JSON文件中删除无效字符?

cbll:

我正在通过命令行读取文件。

由于该文件是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"更“干净”地执行JSON的方法

阿德里安:

此代码有几个重大问题,导致其行为不正常。我在下面的评论中提到了这些:

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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有效地从大.tgz中删除文件

如何有效地从 Scala 中的字符串中删除非单词字符?

有效地使用try()删除文件

有效地从字符串列中删除多个短语

如何有效地删除字符串中连续的重复单词或短语

如何有效地从列表中删除

有效地从nparray中删除零

有效地从 HashMap 中删除列表项

有效地从列表中删除重复项

如何有效地删除单个目录中的2,000,000个文件?

如何有效地从大型文本文件中删除重复行?

有效地从 CSV 文件中删除列的 Python 脚本

有效地搜索JSON文件

如何有效地比较Swift中的字符

如何有效地从大txt文件中仅读取字符串

有效地在Matlab中解析CSV文件

有效地使用JSON流(在Java中)

如何有效地解析C ++中的bigdata json文件(wikidata)?

有效地移动文件

有效地删除LinkedList中其他字符串中包含的字符串

如何有效地删除python中dataframe或csv文件中的所有重复项?

如何删除列表中的字符串并有效地在python中仅保留整数

在Swift中有效地删除字符串中的最后一个单词

如何使用Python有效地从txt格式文件中删除制表符

如何使用Linux或PHP命令有效地从XML文件中删除不必要的空格?

如何有效地删除字符串之前(而不是之后)的所有标签?

在大型日志文件中的 2 个模式之间有效地 grep 字符串

如何有效地排序R中字符串中的字符?

如何有效地删除大文件的第一行?