duan0531
2015-02-21 08:47
浏览 327

您如何使用go-simplejson遍历json文件

I have a JSON file of the form:

{
  "data": {
    "docs": [
      {"key00": "val00", "key01": "val01"},
      {"key10": "val10", "key11": "val11"}
    ]
  }
}

and I would like to convert it to separate JSON docs:

file0.json

{
  {"key00": "val00", "key01": "val01"}
}

file1.json

{
   {"key10": "val10", "key11": "val11"}
}

I can enumerate over the array contents using:

j, _ := ioutil.ReadFile(path)
dec, _ := simplejson.NewFromReader(bytes.NewReader(j))
for i,v := range dec.Get("data").Get("docs").MustArray() {
  out := simplejson.New()

  /* ??? move dec key/value pairs to out ??? */

  b, _ := out.EncodePretty()
  ioutil.WriteFile(outpath, b, 0777)
}

but I'm not sure how to iterate over the key/value pairs within the array entries. It's a nice, succinct library but there don't appear to be a lot of examples and my golang expertise is currently limited.

Any help would be appreciated.. thanks!

图片转代码服务由CSDN问答提供 功能建议

我有以下格式的JSON文件:

  {  
“数据”:{
“文档”:[
 {“ key00”:“ val00”,“ key01”:“ val01”},
 {“ key10”:“ val10”,“ key11”:“  val11“} 
] 
} 
} 
   
 
 

,我想将其转换为单独的JSON文档: < h2> file0.json

  {
 {“ key00”:“ val00”,“ key01”:“ val01”} 
} 
   
 
 

file1.json
  {
 {“ key10”:“ val10”,“ key11”:“ val11”} 
} \  n   
 
 

我可以使用以下方法枚举数组内容:

  j,_:= ioutil.ReadFile(path)  
dec,_:= simplejson.NewFromReader(bytes.NewReader(j))
对于i,v:=范围dec.Get(“ data”)。Get(“ docs”)。MustArray(){
 out:=  simplejson.New()
 
 / * ??? 将dec键/值对移出???  * / 
 
b,_:= out.EncodePretty()
 ioutil.WriteFile(outpath,b,0777)
} 
   
 
 

我不确定如何遍历数组条目中的键/值对。 这是一个不错的,简洁的库,但是似乎没有很多示例,而且我的golang专业知识目前很有限。

任何帮助都将不胜感激..谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duaj39673 2015-02-21 12:30
    已采纳

    You can use simplejson.Set:

    for _, doc := range dec.Get("data").Get("docs").MustArray() {
        out := simplejson.New()
        // doc is an interface{} holding a map, we have to type assert it.
        for k, v := range doc.(map[string]interface{}) {
            out.Set(k, v)
        }
        b, _ := out.EncodePretty()
        fmt.Printf("%s
    ", b)
    }
    

    However in that instance, simplejson is an overkill and using a struct / stdlib is more efficient.

    For completeness sake, the std lib version:

    type DataLayout struct {
        Data struct {
            Docs []map[string]string `json:"docs"`
        } `json:"data"`
    }
    
    func main() {
        var in DataLayout
        err := json.NewDecoder(strings.NewReader(j)).Decode(&in)
        if err != nil {
            log.Fatal(err)
        }
        for _, doc := range in.Data.Docs {
            b, err := json.MarshalIndent(doc, "", "\t")
            if err != nil {
                log.Fatal(err)
            }
            fmt.Printf("%s
    ", b)
        }
    }
    

    play

    Notes:

    • Your json example is wrong, "key10", "val10" should be "key10": "val10".
    • When you're in doubt of how a data structure looks and too lazy to read the code, use fmt.Printf("%#v", doc) to see how it looks like.
    已采纳该答案
    打赏 评论
  • douji4223 2015-02-21 13:03

    Is there any specific reason to use simplejson? A lot of encoding can be done with the standard JSON library.

    Using the standard library you could solve your problem like this:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "io/ioutil"
    )
    
    type Document struct {
        Data DataStruct
    }
    
    type DataStruct struct {
        Docs []interface{}
    }
    
    func main() {
        doc, err := ioutil.ReadFile("./doc.json")
        if err != nil {
            panic(err)
        }
        var document Document
        err = json.Unmarshal(doc, &document)
        if err != nil {
            panic(err)
        }
        for index := range document.Data.Docs {
            b, err := json.Marshal(document.Data.Docs[index])
            if err != nil {
                panic(err)
            }
            err = ioutil.WriteFile(fmt.Sprintf("file%d.json", index), b, 0777)
            if err != nil {
                panic(err)
            }
            fmt.Println(string(b))
        }
    }
    

    The sample code will provide you two files with contents like this:

    {"key00":"val00","key01":"val01"}
    

    and

    {"key10":"val10","key11":"val11"}
    

    However, if you noticed I used the []interface{} notation in the DataStruct, which in general is considered bad practice. You should create a new structure with proper type declarations if your real-world data has any.

    打赏 评论

相关推荐 更多相似问题