简体   繁体   中英

Cannot get a property of a Json-String

I get a Json-String from a table-API:

JS

var data = JSON.stringify($table.bootstrapTable('getSelections'));
console.log(data);

Output:

[{"name":"Chemical Entity Recogniser (ChER)","state":true}]

Now I want to get the value of "name". How do I do this?

You could directly use $table.bootstrapTable('getSelections')[0].name to get the name value

or if you still want to use stringify, convert it into a JSON-string and fetch the name value from that you could do like this:

var data = JSON.stringify($table.bootstrapTable('getSelections'));
var obj = JSON.parse(data);
console.log(obj[0].name);

Output: Chemical Entity Recogniser (ChER)

Hope this helps!

The result of

$table.bootstrapTable('getSelections')

is just a plain old JavaScript object. It's not JSON. There is no need to stringify it. Stringification is for turning an object into a string in order to store it or send it somewhere. That's not what you want to do. Just access the value you want directly from the JavaScript object:

var data = $table.bootstrapTable('getSelections');
data[0].name

If you unnecessarily stringify this response as you are doing, then you're just going to have to turn around and parse it again, as your "accepted" answer incorrectly suggests, which will do nothing more than give you back the object you started with in the first place.

Try using:

var o = JSON.parse(data)

JSON.parse() convert an JSON object to Javascript object...

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