dsf4354353452 2019-03-03 17:29
浏览 80
已采纳

解析嵌套的json文件

I'm parsing a JSON file with an unusual structure which looks like this:

{
    "394885": 
    {
        "record": 
        {
            "student_name": "Daryl Jones",
            "student_number": 123884,
            "student_dob": "12/10/1982",
            "student_email": "djones@school.ac.uk",
        }    
    },
}

I've been working through some code demos, but I would like to put it all in to a struct, which I then plan to search through by the number string that's the object name I would guess?

I'm not strong on JSON or Go, and this is the code I've written so far:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

type id struct {
    recordid string
    record   []record
}

type record struct {
    name   string
    number uint32
    dob    string
    email  string
}

func main() {
    jsonFile, err := os.Open("/home/emyrw/development/go/src/json_processor/demo.json")
    if err != nil {
        fmt.Println(err)
    } else {
        var records id

        byteValue, _ := ioutil.ReadAll(jsonFile)
        json.Unmarshal(byteValue, &records)
        fmt.Println(records)

        fmt.Println("opened demo.json")
        defer jsonFile.Close()
    }
}

I'm not sure I've got it right, but would value any tips or advice anyone has to offer. I've been googling, but none of the samples I find quite fit my scenario.

  • 写回答

4条回答 默认 最新

  • doujiang2643 2019-03-03 17:50
    关注

    You can use this struct for your id type

    type id struct {
        record map[string]record
    }
    

    Edit

    here is the working solution with some explanation:

    since you have a multi level json, you can parse it into nested structs.

    {
            "student_name": "Daryl Jones",
            "student_number": 123884,
            "student_dob": "12/10/1982",
            "student_email": "djones@school.ac.uk"
    } 
    

    to parse this part of the json you need this struct

    type record struct {
        Name   string `json:"student_name"`
        Number uint32 `json:"student_number"`
        Dob    string `json:"student_dob"`
        Email  string `json:"student_email"`
    }
    

    the fields have to be exported(start with capital letter) and have a json tag that matches json properties.

    {
        "record": 
        {
            "student_name": "Daryl Jones",
            "student_number": 123884,
            "student_dob": "12/10/1982",
            "student_email": "djones@school.ac.uk"
        }    
    }
    

    for this part to work, you need a nested struct like this

    type id struct {
         Record record
    }
    

    the filed name is exported again but since it matches your json property, you don't need the tag.

    {
    "394885": 
      {
        "record": 
        {
            "student_name": "Daryl Jones",
            "student_number": 123884,
            "student_dob": "12/10/1982",
            "student_email": "djones@school.ac.uk"
        }    
      }
    }
    

    since the top level property name is the student id, you can use a map instead of a struct

     var records map[string]id 
    

    and make sure you don't have trailing commas as that is not allowed in json specs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题