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条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算