donglou8371 2018-07-10 20:58
浏览 145
已采纳

进行JSON解析错误

I am writing my first program in Golang, or rather modifying a template, if you will. Want to parse values from a yaml file.

apiVersion: "backend.example.com/v1alpha1"
kind: "Example"
metadata:
  name: "solar-demo"
spec:
  size: 2
  group: backend.example.com
  names:
    kind: Example
    listKind: ExampleList
    plural: solar-demos
    singular: solar-demo
  scope: Namespaced
  version: v1alpha1
  pods:
    - name: api
      image: "shmukler/docker_solar-api"
      port: 3000
      command: '{"npm", "run", "start-api"}'
    - name: service
      image: "shmukler/docker_solar-svc"
      port: 3001
      command: '{"npm", "run", "start-solar-svc"}'

To parse-out variables, I do:

type Example struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata"`
    Spec              ExampleSpec   `json:"spec"`
    Status            ExampleStatus `json:"status,omitempty"`
}
type ExampleSpec struct {
    Size int32 `json:"size"`
    //  Pods []ExamplePod `json:"pods"`
}
type ExampleStatus struct {
    Nodes []string `json:"nodes"`
}
type ExamplePod struct {
    Name    []string `json:"name"`
    Image   []string `json:"image"`
    Port    int32    `json:"port"`
    Command []string `json:"command"`
}

If I comment out the Pods []ExamplePod line, the code works albeit without parsing the Pods array. If I leave it in, there is an error, as soon as I pass the yaml document - Observed a panic: &errors.errorString{s:"failed to decode json data with gvk...

I want to stick pods into an array of structures of the type ExamplePod.

Today is my first day working with Go language. Sorry for a stupid question. My yaml, the relevant fragment in JSON representation.

spec":{  
   "group":"backend.example.com",
   "names":{  
      "kind”:”Example”,
      "listKind”:”ExampleList",
      "plural":"solar-demos",
      "singular":"solar-demo"
   },
   "pods":[  
      {  
         "command":"{\"npm\", \"run\", \"start-api\"}",
         "image":"shmukler/docker_solar-api",
         "name":"api",
         "port":3000
      },
      {  
         "command":"{\"npm\", \"run\", \"start-solar-svc\"}",
         "image":"shmukler/docker_solar-svc",
         "name":"service",
         "port":3001
      }
   ],
   "scope":"Namespaced",
   "size":2,
   "version":"v1alpha1"
}

I am able to get example.Spec.Size just fine. What I am missing is the pods array.

  • 写回答

3条回答 默认 最新

  • duanjiaonie6097 2018-07-12 09:26
    关注

    Using the suggestions from both offered answers changed the YAML to:

    containers:
    - name: api
      image: "shmukler/docker_solar-api"
      port: 3000
      command: ["npm", "run", "start-api"]
    - name: service
      image: "shmukler/docker_solar-svc"
      port: 3001
      command: ["npm", "run", "start-solar-svc"]
    

    Then, updated definitions to:

    type Spec struct {
        Size       int32               `json:"size"`
        Containers []Container `json:"containers"`
    }
    type Status struct {
        Nodes []string `json:"nodes"`
    }
    type Container struct {
        Name    string   `json:"name"`
        Image   string   `json:"image"`
        Port    int32    `json:"port"`
        Command []string `json:"command"`
    }
    

    That finally worked fine.

    Thank you for the answers, helped me to understand how Golang type definitions work.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?