如何使用jQuery通过以下JSON转储从 fields 键获取所有值:
{
"fields": [
{
"label": "Name",
"field_type": "text",
"required": true,
"field_options": {
"size": "medium"
},
"cid": "c1"
}, {
"label": "Email",
"field_type": "text",
"required": true,
"field_options": {
"size": "medium"
},
"cid": "c2"
}
]
}
我想要的只是获得:
{
"label": "Name",
"field_type": "text",
"required": true,
"field_options": {
"size": "medium"
},
"cid": "c1"
}, {
"label": "Email",
"field_type": "text",
"required": true,
"field_options": {
"size": "medium"
},
"cid": "c2"
}
答案 0 :(得分:0)
将主json存储到变量中。您可以使用以下方式访问其密钥:
variable.fields
答案 1 :(得分:0)
var originalJson = '{"fields":[{"label":"Name","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c1"},{"label":"Email","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c2"}]}'
var fieldsJson = JSON.parse(originalJson).fields;
如果你想将它作为json字符串添加
var fieldsJsonString = JSON.stringify(fieldsJson)
答案 2 :(得分:0)
var o = {"fields":[{"label":"Name","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c1"},{"label":"Email","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c2"}]};
var labels = o.fields
然后,您可以通过for
。
for(var i = 0; i < labels.length; i++){
console.log(labels[i]);
}
答案 3 :(得分:0)
当我使用
时var obj = jQuery.parseJSON(payload);
console.log(obj.fields);
我明白了:
[Object {label =“Email”,field_type =“text”,required = true,more ...},Object {label =“Name”,field_type =“text”,required = true,more ... },Object {label =“Subject”,field_type =“text”,required = true,more ...},Object {label =“Message”,field_type =“paragraph”,required = true,more ...}]
如何获得:
{"label":"Name","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c1"},{"label":"Email","field_type":"text","required":true,"field_options":{"size":"medium"},"cid":"c2"}