dongzhi8487 2019-03-21 15:34
浏览 67
已采纳

JSON解组数组不起作用,但是相同的数据在javascript上也有效

I try to have some array values into JSON string where I send to the browser and in the browser is working fine to add some input fields dynamically, but when I try to check the data from that new fields on Go I try to unmarshal the same data but is not working because the values are empty.

This is the code:

package main

import "fmt"
import "encoding/json"


type PublicKey struct {
        Name        string   `json:"name"`
        Type        string   `json:"type"`
    Description string   `json:"description"`
    Values      []string `json:"values"`
}


func main() {

keysBody := []byte(`    
[
   [
      {
         "name":"fecha_inicio",
         "type":"date",
         "description":"Fecha de Inicio",
         "values":[
            ""
         ]
      }
   ],
   [
      {
         "name":"fecha_final",
         "type":"date",
         "description":"Fecha Final",
         "values":[
            ""
         ]
      }
   ],
   [
      {
         "name":"username",
         "type":"select",
         "description":"Usuario",
         "values":[
            "admin",
            "gus"
         ]
      }
   ]
]
`)


    keys := make([]PublicKey,0)
    json.Unmarshal(keysBody, &keys)
    fmt.Printf("%#v", keys)




}

https://play.golang.org/p/kKT3IN4_8vb

This is the result:

[]main.PublicKey{main.PublicKey{Name:"", Type:"", Description:"", Values:[]string(nil)}, main.PublicKey{Name:"", Type:"", Description:"", Values:[]string(nil)}, main.PublicKey{Name:"", Type:"", Description:"", Values:[]string(nil)}}

This is the same code in javascript working fine:

parameterData contains the same JSON string

        var jsonMenus = JSON.parse(parameterData);     
        for (let i = 0; i < jsonMenus.length; i++) {
            let arr = jsonMenus[i];
            for (let j = 0; j < arr.length; j++) {
                //New DIV
                var newDiv = document.createElement("div");
                newDiv.setAttribute("class","w3-quarter");
                //console.log(arr[j].name + ' ' + arr[j].type + ' ' + arr[j].description);
                var label = document.createElement("Label");
                label.innerHTML = arr[j].description;
                label.setAttribute("class","w3-label");
                newDiv.appendChild(label);
                if (arr[j].type != 'select') {
                    var input = document.createElement("input");
                    input.type = arr[j].type;
                    input.name = arr[j].name;
                    input.setAttribute("class","w3-input w3-border w3-round");
                    newDiv.appendChild(input);
                } else {
                    var select = document.createElement("select");
                    select.name = arr[j].name;
                    var values = arr[j].values
                    for (let k = 0; k < values.length; k++) {
                        opt = document.createElement('option');
                        opt.value = values[k];
                        opt.innerHTML = values[k];
                        select.appendChild(opt);
                    }
                    select.setAttribute("class","w3-input w3-border w3-round");
                    newDiv.appendChild(select);
                }
                container.appendChild(newDiv);

Can anyone can help to have the JSON string working in both places?

  • 写回答

1条回答 默认 最新

  • douluoyou9876 2019-03-21 15:55
    关注

    Your json is a 2 dimensional array but you are trying to unmarshal to a single dimension array.

    You need to unmarshal to an [][]PublicKey, here is the adjusted playground: https://play.golang.org/p/ykzzqtSPJCU.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题