简体   繁体   English

如何在json中访问数组元素值

[英]How can I access array element value in json

I'm not able to access _sValues in my html page, however i can able to console.log the interested _sValues array element in console. 我无法在html页面中访问_sValues,但是我可以在console中console.log感兴趣的_sValues数组元素。

JSON JSON

[
    {  
      "_this":{  
         "_isEvent":false,
         "_sNames":[  
            "name",
            "tag",
            "notes",
            "input",
            "type",
            "action"
         ],
         "_sValues":[  
            "Testing01",
            "#13",
            "1504013826",
            "No details",
            "cType",
            "NA"
         ],
         "_cName":"namesList",
         "_dName":"TEST",
         "_id":"12345",
      }
   }
]

HTML HTML

<th class="col-md-5 col-xs-5" data-field="_this._sValues[1]" data-sortable="true">S-VALUES</th>

I would like to see #13 displayed on the page instead i don't see any values or console error. 我想看到页面上显示的#13相反,我没有看到任何值或控制台错误。 However i can display all values by doing _this.sValues 但是我可以通过_this.sValues显示所有值

SCRIPT 脚本

var data;
$(function () {
    $.getJSON("http://localhost:8080/services/webapi/getAllData", function(json){
       data = json;
       $('#table').bootstrapTable({
          data: data
       });
    });
});

DEV TOOL 开发工具

data[0]._this._sValues[1]
"#13"

In your case, i assume you only have one row. 就您而言,我假设您只有一行。

$(".col-md-5").attr("data-field",data[0]._this._sValues[1]);

It will inject the data to html. 它将数据注入html。 If you have multi row, you can use for loop. 如果您有多行,则可以使用for循环。

I believe you have a silent error. 我相信您有一个无声的错误。 It has to do with the trailing comma in your data object. 它与数据对象中的尾部逗号有关。

 "_id":"12345",

// should be

 "_id":"12345"

That's all I noticed. 这就是我所注意到的。 I Courbet wrong but I know trailing commas mess with some browsers. 我Courbet错了,但我知道某些浏览器的结尾逗号会弄乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM