我尝试将一些数组值放入发送到浏览器的JSON字符串中,并且在浏览器中可以正常动态添加一些输入字段,但是当我尝试在Go上检查来自该新字段的数据时,我尝试解组相同的数据,但是由于这些值为空而无法正常工作。
这是代码:
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
这是结果:
[]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)}}
这与javascript中正常工作的代码相同:
parameterData包含相同的JSON字符串
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);
任何人都可以帮助使JSON字符串在两个地方都能正常工作吗?