简体   繁体   中英

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.

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"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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