The simplest one for me will do it in java or go.
In Java:
- you can read the file line after the line with
readLine
using new BufferedReader(new FileReader(file))
- you can fill the
HashMap
HashMap<String,String>
while reading - create
new BufferedWriter(new FileWriter(outputfilepath))
- using gson you just need to use
this is:
Gson gson = new Gson(); gson.toJson(myList, myFileOutputStreamWriter);
In Go:
You do not need to import an external package, Go includes the necessary ones.
It will be something like this (some other error checks will 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) }
source share