0

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?

5
  • You need to deserialize the data if you want to get any information out of it Commented Jul 8, 2015 at 16:25
  • 2
    Why use .stringify() if you want to get values from the data? Commented Jul 8, 2015 at 16:28
  • I use it from the example here: issues.wenzhixin.net.cn/bootstrap-table/#methods/… Commented Jul 8, 2015 at 16:44
  • @torazaburo data[0].name will give undefined values. The output is a JSON string and need to be parsed! Please stop down-voting without a valid answer! Commented Jul 8, 2015 at 18:12
  • @torazaburo I refer to the result of JSON.stringify as the "output". The interest of the post is that he got his "Json-String" and he needs the value of "name" out of it. I just showed him the way to do it!. Can you please review your downvote? Commented Jul 8, 2015 at 18:24

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

4 Comments

Why are you stringifying and then immediately parsing?
JSON.stringify turns an object in to a JSON text and stores that JSON text in a string. JSON.parse turns a string of JSON text into an object.
Right, so the effect of JSON.stringify followed by JSON.parse is a no-op. Why are you suggesting doing a no-op? Your obj is precisely equivalent to what you started off with, namely $table.bootstrapTable('getSelection').
@torazaburo I agree!. I have edited my post. Is it fine now?
1

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.

Comments

0

Try using:

var o = JSON.parse(data)

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

2 Comments

o gives me the output 'o[object Object]'
so how to get the value of the name attribute?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.