将文本转换为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11265575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting text to JSON
提问by Tomer Lichtash
How would you recommend converting a text file to JSON format?
您建议如何将文本文件转换为 JSON 格式?
I have a text file with about 500 bits of text in the following format:
我有一个包含大约 500 位文本的文本文件,格式如下:
[number in brackets or astriek]
[line1]
[line2]
[line3]
[space]
.
.
.
I want to convert it to JSON, like so:
我想将其转换为 JSON,如下所示:
"page1": {
"line1": "LINE1",
"line2": "LINE2",
"line3": "LINE3"
},
"page2": {
"line1": "LINE1",
"line2": "LINE2",
"line3": "LINE3"
}
.
.
.
Ideas?
想法?
回答by rkyser
You could use Gelatin.
你可以用明胶。
You'd use a grammar to define your input text (can be a little difficult if you've never done it before). Then you just run your text file through Gelatin with your grammar file, and specify the output.
您将使用语法来定义您的输入文本(如果您以前从未这样做过,可能会有点困难)。然后,您只需使用语法文件通过 Gelatin 运行文本文件,并指定输出。
Edit 1: It would be helpful if you would post a snippet of what you are trying to convert.
编辑 1:如果您发布一段您要转换的内容的片段会很有帮助。
回答by Denys Séguret
The simplest for me would be do to it in java or go.
对我来说最简单的方法是用 java 或 go 来做。
In Java :
在 Java 中:
- you can read a file line after line with
readLineusing anew BufferedReader(new FileReader(file)) - you can fill a
HashMapofHashMap<String,String>during the reading - create a
new BufferedWriter(new FileWriter(outputfilepath)) - using gson, you then just have to use
- 您可以
readLine使用new BufferedReader(new FileReader(file)) - 您可以填写一个
HashMap的HashMap<String,String>在读期间 - 创建一个
new BufferedWriter(new FileWriter(outputfilepath)) - 使用gson,然后你只需要使用
this :
这个 :
Gson gson = new Gson();
gson.toJson(myList, myFileOutputStreamWriter);
In Go :
在围棋中:
You don't need to import an external package, Go includes the needed ones.
您不需要导入外部包,Go 包含所需的包。
This would be something like this (some other error testing would be good) :
这将是这样的(其他一些错误测试会很好):
package main
import (
"bufio"
"fmt"
"io"
"encoding/json"
"log"
"strings"
"os"
)
func main() {
myBigThing := make(map[string]map[string]string)
f, _ := os.Open("/home/dys/dev/go/src/tests/test.go")
r := bufio.NewReader(f)
var currentPage map[string]string
pageNum := 0
for {
line, err := r.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Println("Error in parsing :", err)
}
break
}
if currentPage==nil {
currentPage = make(map[string]string)
myBigThing[fmt.Sprintf("page%d",pageNum)] = currentPage
pageNum++
} else if line=="" {
currentPage = nil
} else {
tokens := strings.Split(line, ":")
if len(tokens)==2 {
currentPage[tokens[0]] = tokens[1]
}
}
}
f, err := os.Create("/home/dys/test.json")
if err != nil {
log.Println("Error :", err)
return
}
defer f.Close()
bout, _ := json.Marshal(myBigThing)
f.Write(bout)
}
回答by Vedha Peri
Using Visual Studio
使用 Visual Studio
If you have the required data in a text file, this will be the best option.
如果您在文本文件中有所需的数据,这将是最佳选择。
Open Visual Studio and under File menu --> New --> File Under the installed, you should have the "Web" option. One of the formats listed there is JSON.
打开 Visual Studio,在 File 菜单下 --> New --> File 安装下,你应该有“Web”选项。此处列出的格式之一是 JSON。
Select that and Copy and Paste your text document in VS. Save the file and it is in JSON format.
选择它并在 VS 中复制和粘贴您的文本文档。保存文件,它是 JSON 格式。

