Is there a way to loop over a json file and get each struct into a separate file — Golang?

huangapple go评论118阅读模式
英文:

Is there a way to loop over a json file and get each struct into a separate file -- Golang?

问题

所以我的程序是读取一个CSV文件并将其转换为JSON。我已经能够做到这一点,并将JSON写入文件,但我希望能够将该JSON文件中的每个结构或对象写入另一个文件。

我附上了我的代码以便更好地理解:

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/csv"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "strconv"
  12. )
  13. type NftRecord struct {
  14. SeriesNumber int `json:"Series Number"`
  15. Name string `json:"Name"`
  16. Hash string `json:"Hash"`
  17. UUIDs string `json:"UUIDs"`
  18. Description string `json:"Description"`
  19. DriveLink string `json:"Drive Link"`
  20. }
  21. func createRecordsList(data [][]string) []NftRecord {
  22. var recordList []NftRecord
  23. for i, line := range data {
  24. if i > 0 {
  25. var rec NftRecord
  26. for j, field := range line {
  27. if j == 0 {
  28. var err error
  29. rec.SeriesNumber, err = strconv.Atoi(field)
  30. if err != nil {
  31. continue
  32. }
  33. } else if j == 4 {
  34. rec.Name = field
  35. } else if j == 7 {
  36. rec.Description = field
  37. } else if j == 6 {
  38. rec.UUIDs = field
  39. } else if j == 5 {
  40. rec.Hash = field
  41. } else if j == 8 {
  42. rec.DriveLink = field
  43. } else if j == 13 {
  44. var err error
  45. if err != nil {
  46. continue
  47. }
  48. }
  49. }
  50. recordList = append(recordList, rec)
  51. }
  52. }
  53. return recordList
  54. }
  55. func main() {
  56. f, err := os.Open("./scaler.csv")
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. defer f.Close()
  61. csvReader := csv.NewReader(f)
  62. data, err := csvReader.ReadAll()
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. recordList := createRecordsList(data)
  67. jsonData, err := json.MarshalIndent(recordList, "", " ")
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. _ = ioutil.WriteFile("./test.json", jsonData, 0644)
  72. fmt.Println(string(jsonData))
  73. //hashing
  74. hash := sha256.New()
  75. if _, err := io.Copy(hash, f); err != nil {
  76. panic(err)
  77. }
  78. sum := fmt.Sprintf("%x", hash.Sum(nil))
  79. fmt.Println(sum)
  80. }

我已经尝试了几乎所有的方法,希望能够解决这个问题,因为我还有其他功能要添加。

这是下面的JSON:

  1. {
  2. "Series Number": 301,
  3. "Name": "wendy-the -banker",
  4. "Hash": "D615D6E161E5F820A2B9F5C3ED3867BE0AA9F16DDCA00A0550E30E29A0B467AD",
  5. "UUIDs": "587645e2-5ace-11ed-9b6a-0242ac120002",
  6. "Description": "Wendy喜欢在银行工作,因为她可以遇到不同的人。",
  7. "Drive Link": "Link"
  8. },
  9. {
  10. "Series Number": 302,
  11. "Name": "uduak-the -dark horse",
  12. "Hash": "5A63A94F455D367EC0AA39F57F827DDBECD3F335C817ED5A76460A2AB8E9068A",
  13. "UUIDs": "58764862-5ace-11ed-9b6a-0242ac120002",
  14. "Description": "Uduak看起来很无辜,但整个公寓都被窃听了",
  15. "Drive Link": "Link"
  16. }

希望对你有帮助!

英文:

So my program is to read through a csv file and convert it into json. i've been able to do that and write the json to a file however i want to be able to take each struct or object from this json file and write to a file.

i've attached my code for more understanding

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/csv"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "strconv"
  12. )
  13. type NftRecord struct{
  14. SeriesNumber int `json:"Series Number"`
  15. Name string `json:"Name"`
  16. //TheDescriptor string `json:"The Descriptor"`
  17. //NewName string `json:"New Name"`
  18. Hash string `json:"Hash"`
  19. UUIDs string `json:"UUIDs"`
  20. Description string `json:"Description"`
  21. DriveLink string `json:"Drive Link"`
  22. }
  23. func createRecordsList(data [][]string) []NftRecord{
  24. var recordList []NftRecord
  25. for i, line := range data{
  26. if i > 0{
  27. var rec NftRecord
  28. for j, field := range line{
  29. if j == 0{
  30. var err error
  31. rec.SeriesNumber, err = strconv.Atoi(field)
  32. if err != nil{
  33. continue
  34. }
  35. }else if j == 4{
  36. rec.Name = field
  37. }else if j == 7 {
  38. rec.Description = field
  39. }else if j ==6{
  40. rec.UUIDs = field
  41. }else if j == 5{
  42. rec.Hash = field
  43. }else if j == 8{
  44. rec.DriveLink = field
  45. } else if j == 13{
  46. var err error
  47. if err != nil{
  48. continue
  49. }
  50. }
  51. }
  52. recordList = append(recordList, rec)
  53. }
  54. }
  55. return recordList
  56. }
  57. func main(){
  58. f, err := os.Open("./scaler.csv")
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer f.Close()
  63. csvReader := csv.NewReader(f)
  64. data, err := csvReader.ReadAll()
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. recordList := createRecordsList(data)
  69. jsonData, err := json.MarshalIndent(recordList, "", " ")
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. _ = ioutil.WriteFile("./test.json", jsonData, 0644)
  74. fmt.Println(string(jsonData))
  75. //hashing
  76. hash := sha256.New()
  77. if _, err := io.Copy(hash, f); err != nil {
  78. panic(err)
  79. }
  80. sum := fmt.Sprintf("%x", hash.Sum(nil))
  81. fmt.Println(sum)
  82. }

i've tried pretty much everything i guess, i'm hoping i could get past this as i've got other features to add.

This is the JSON below

  1. {
  2. "Series Number": 301,
  3. "Name": "wendy-the -banker",
  4. "Hash": "D615D6E161E5F820A2B9F5C3ED3867BE0AA9F16DDCA00A0550E30E29A0B467AD",
  5. "UUIDs": "587645e2-5ace-11ed-9b6a-0242ac120002",
  6. "Description": "Wendy loves working in the bank, as she gets to meet different people.",
  7. "Drive Link": "Link"
  8. },
  9. {
  10. "Series Number": 302,
  11. "Name": "uduak-the -dark horse",
  12. "Hash": "5A63A94F455D367EC0AA39F57F827DDBECD3F335C817ED5A76460A2AB8E9068A",
  13. "UUIDs": "58764862-5ace-11ed-9b6a-0242ac120002",
  14. "Description": "Uduak looks innocent and has the whole apartment bugged",
  15. "Drive Link": "Link"
  16. },

答案1

得分: 1

循环遍历recordList。对于每个元素,将其转换为JSON并写入一个唯一的文件。你需要有一个唯一的文件命名方案,但一个简单的实现方式是使用计数器(例如1.json,2.json等)。

英文:

Loop over recordList. For each element marshall it to JSON and write it to a unique file. You’ll have to have a unique file naming scheme but a trivial one to implement would be a counter (eg 1.json, 2.json, etc).

huangapple
  • 本文由 发表于 2022年11月3日 16:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74299944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定