cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Convert Text String to JSON object

Former Member
0 Likes
4,405

Hi,

How can a string be converted to a JSON object in SAPUI5 ?

Example : var mystr = ' [ { name : "Joe", last : "Doe"}, { name : "Jack", last : "Daniel" }]';

How do i convert mystr to a JSON object which can be used in a JSONModel?

$,parseJSON(mystr) or jQuery.parseJSON(myStr) do not seem to exist.

JSONModel.setJSON(mystr) too did not work.

Any pointers ?

regards,

sreeram

View Entire Topic
Former Member
0 Likes

Simply use


var jsonObject = eval(mystr);

Former Member
0 Likes

Please do not!

Reasons why you should avoid eval are explained here:

How evil is eval? | JavaScript, JavaScript...

Former Member
0 Likes

You're right. However, the jQuery.parseJSON function will not work for the example above, as it is not a valid JSON. It's an array object of JSON sub objects.