How can I access array element value in json

52 views Asked by At

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.

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

<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. However i can display all values by doing _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"
2

There are 2 answers

0
Huy Nguyen On

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. If you have multi row, you can use for loop.

0
andre mcgruder On

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.