-1

how to convert in json object?
I also tried the following.

 $scope.getEval = function (str) {
                return eval(str);
 };
        try {
            s = $scope.getEval("\nexports = { emptyFunction: function () {  \n      exit();    },    getUserName: function () {        if (!variable.global_name) {            exit(changeFlowAndStep('get_user_name', 0, {global_first_time_done: false}, true));        } else {            exit({variable:{global_first_time_done: true}});        }    }}")

            for (var i in s) {
                s[i] = s[i].toString();
            }
            $scope.s = s;
            console.log($scope.s)


        } catch (e) {
            console.log(e);
        }
Adder
  • 5,708
  • 1
  • 28
  • 56
  • Is `exports = { emptyFunction: function () ...` the string you want to convert from json to object? – Adder Jun 12 '17 at 11:34

2 Answers2

1

If you have a javascript object, it is easy to generate a JSON string with JSON.stringify() and if you have a JSON string, it is easy to generate a javascript object with JSON.parse()

// going from JSON to JS object...

var json = '{"result":true,"count":1}',
    obj = JSON.parse(json);

console.log(obj);
console.log(obj.count);


// from JS object to JSON string...

var str = JSON.stringify(obj);
console.log(str);

from:

tgogos
  • 23,218
  • 20
  • 96
  • 128
1

Your use of eval works, as you can see when you call console.dir($scope.s);

It is possible to use json = JSON.stringify(obj); and obj = JSON.parse(json);. However it appears JSON.stringify ignores object fields with a function as a value. That is your string is not a useful json string.

Adder
  • 5,708
  • 1
  • 28
  • 56