javascript json 获取字段名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14386121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json get field name
提问by Rob
I want to push the Field Names into the option value and the result into the text of a select. It should look like this:
我想将字段名称推送到选项值中,并将结果推送到选择文本中。它应该是这样的:
<select id="ddl_fields">
<option value="RoleId">e407d28a</option>
<option value="RoleName">Sales</option>
</select>
This is the json object returned from the database:
这是从数据库返回的json对象:
"[{"RoleId":"e407d28a","RoleName":"Sales"}]"
This is the code and it pulls back a valid result:
这是代码,它拉回了一个有效的结果:
function getFields(){
var the_id = $(".hid_ID").val();
var jsonText = JSON.stringify({ id: the_id });
$.ajax({
type: "POST",
url: "bc_Admin.aspx/getFields",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data.d != "0") {
var obj = $.parseJSON(data.d);
//what needs to change???
$.each(obj, function (index, value) {
$('#ddl_fields')
.append($("<option value=" + value.id_Role + ">" + value.RoleName + "</option>"));
});
}
} //end success
});
}
Similar to this questions but need a JQuery solution. How to get/list all field names of a JSON data with ExtJS?
类似于这个问题,但需要一个 JQuery 解决方案。 如何使用 ExtJS 获取/列出 JSON 数据的所有字段名称?
Thanks!
谢谢!
采纳答案by Musa
Assuming that data.d
is in the format you posted obj
is an array and you want to iterate the object in the array and not the array itself.
假设data.d
您发布的格式obj
是一个数组,并且您想要迭代数组中的对象而不是数组本身。
$.each(obj[0], function (index, value) {
$('#ddl_fields')
.append($("<option value=" + index + ">" + value + "</option>"));
});
回答by Martín Valdés de León
You have to iterate through the keys of the object, and use hasOwnProperty
to make sure its a key of the object and not of its prototype.
您必须遍历对象的键,并使用它hasOwnProperty
来确保它是对象的键而不是其原型的键。
var key, keys = [];
for (key in obj) {
if (obj.hasOwnProperty(key))
keys.push(key)
}
回答by jholloman
You'll want to loop through the object and use hasOwnProperty().
您需要遍历对象并使用 hasOwnProperty()。
for (var i in inputData) {
if (inputData.hasOwnProperty(i)) {
console.log(i + " , " + inputData[i]);
}}
This example will just log to console key: value.
此示例将仅登录到控制台键:值。
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
回答by Louis XIV
Not sure if I understand, but here's what I would do to get what you want (I made also your code simplier):
不确定我是否理解,但这是我将如何获得您想要的东西(我还简化了您的代码):
function getFields(the_id){
$.ajax({
type: "POST",
url: "bc_Admin.aspx/getFields",
data: { id : the_id },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
for(var i = 0; i < data.length; i++){
console.log(i + ": " + data[i]);
}
},
error : function(s , i , error){
console.log(error);
}
});
}
回答by Gabriele Petrioli
Just change
只是改变
$.each(obj, function (index, value) {
to
到
$.each(obj[0], function (index, value) {
The problem is that you return an array in your json
问题是你在你的 json 中返回一个数组