简体   繁体   English

将来自多个单元格的数据合并到一个JSON对象中

[英]Combining data from multiple cells into one JSON object

I am trying to combine data from multiple cells from an excel spreadsheet into one JSON encoded string. 我正在尝试将来自Excel电子表格中多个单元格的数据合并为一个JSON编码的字符串。 I cannot figure out how to do so, the code below is creating a new JSON object per cell. 我不知道该怎么做,下面的代码正在为每个单元格创建一个新的JSON对象。 How do I differentiate the cells to combine into the same JSON string? 如何区分单元格以合并为相同的JSON字符串?

package main

import (
    "fmt"
    "github.com/tealeg/xlsx"
    "encoding/json"

)

func main() {
    excelFileName := "/Users/isaacmelton/Desktop/Test_Data.xlsx"
    xlFile, err := xlsx.OpenFile(excelFileName)
    if err != nil {
        fmt.Printf("Cannot parse data")
    }
    for _, sheet := range xlFile.Sheets {
        for _, row := range sheet.Rows {

            fmt.Printf("\n")
            for x, cell := range row.Cells {
                if x == 3 || x == 5 {
                    data := map[string]string{"d_name": cell.String(), "name": cell.String()}
                    json_data, _ := json.Marshal(data)

                    fmt.Println(string(json_data))
                }
            }
        }
    }
}

Running the above code results in the following: 运行上面的代码将导致以下结果:

{"foo":"cell1","bar":"cell1"}
{"foo":"cell2","bar":"cell2"}

I expect something like this: 我期待这样的事情:

{"foo":"cell1", "bar":"cell2"}

If I right understand your request you just need to define root element, add cells into it and marshal this element rather than individual cells. 如果我正确理解您的请求,则只需定义根元素,在其中添加单元格并编组此元素,而不是单个单元格。

root := []map[string]string{}
for x, cell := range row.Cells {
    if x == 3 || x == 5 {
        root = append(root, map[string]string{"d_name": cell.String(), "name": cell.String()})
    }
}
json_data, _ := json.Marshal(root)
fmt.Println(string(json_data))

http://play.golang.org/p/SHnShHvW_0 http://play.golang.org/p/SHnShHvW_0

You may use 你可以用

a, err := row.Cells[3].String()
b, err := row.Cells[5].String()

Like this working code: 像这样的工作代码:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/tealeg/xlsx"
)

func main() {
    xlFile, err := xlsx.OpenFile(`Test_Data.xlsx`)
    if err != nil {
        panic(err)
    }
    for _, sheet := range xlFile.Sheets {
        for _, row := range sheet.Rows {
            //for x, cell := range row.Cells {
            //if x == 3 || x == 5 {
            a, err := row.Cells[3].String()
            if err != nil {
                panic(err)
            }
            b, err := row.Cells[5].String()
            if err != nil {
                panic(err)
            }
            data := map[string]string{"d_name": a, "name": b}
            json_data, err := json.Marshal(data)
            if err != nil {
                panic(err)
            }
            fmt.Println(string(json_data))
            //}
            //}
        }
    }
}

output: 输出:

{"d_name":"1000","name":"a"}
{"d_name":"2000","name":"b"}
{"d_name":"3000","name":"c"}
{"d_name":"4000","name":"d"}
{"d_name":"5000","name":"e"}

input file content: 输入文件内容:

1   10  100 1000    10000   a
2   20  200 2000    20000   b
3   30  300 3000    30000   c
4   40  400 4000    40000   d
5   50  500 5000    50000   e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM