-2

So I'm receiving this string:

{"id":"0-worfebvjyyvqjjor","size":17,"price":921,"face":"( .-.)","date":"Mon Jan 04 2016 22:55:30 GMT+0000 (GMT Standard Time)"}
{"id":"1-ifma3yxxccgzaor","size":19,"price":98,"face":"( .o.)","date":"Fri Jan 08 2016 16:11:25 GMT+0000 (GMT Standard Time)"}
{"id":"2-sa3iurvt4hv0lik9","size":14,"price":659,"face":"( `·´ )","date":"Sun Jan 03 2016 06:20:28 GMT+0000 (GMT Standard Time)"}
{"id":"3-bc3tf55q9vx11yvi","size":33,"price":361,"face":"( ° ͜ ʖ °)","date":"Fri Jan 01 2016 22:49:22 GMT+0000 (GMT Standard Time)"}

here in console.log(data):

var WareHouseResource = $resource('/api/products?limit=10', {}, {
    query: {
        method: 'GET',
        isArray: false,
        transformResponse: function(data) {
            console.log(data);
        }
    }
});

How do I convert data into a JSON array?? I already tried JSON.parse(data) but it throws an error.

Frederico Jesus
  • 649
  • 6
  • 14
  • Looks like you're missing comas after each object – Chanthu Jan 16 '16 at 05:42
  • Possible duplicate of [AngularJS - Any way for $http.post to send request parameters instead of JSON?](http://stackoverflow.com/questions/12190166/angularjs-any-way-for-http-post-to-send-request-parameters-instead-of-json) – CandleCoder Jan 16 '16 at 05:43
  • 1
    send proper json in the first place. Fix your back end – charlietfl Jan 16 '16 at 06:14
  • It's an exercise, and I was told to not change the backend – Frederico Jesus Jan 16 '16 at 06:20
  • Unless you have correct JSON, you are not going to be success here . Tell whomever that your api is wrong . As @charlietfl said correct you api first. For the question asked here answer is "angular.fromJson()" – Midhun Murali Jan 16 '16 at 06:28
  • Then do it yourself and stop getting people here to do your homework for you. It doesn't appear you made any effort at all to solve this yourself – charlietfl Jan 16 '16 at 06:31

2 Answers2

1

If you want the data as an array, you might wanna set isArray to true

var WareHouseResource = $resource('/api/products?limit=10', {}, {
    query: {
        method: 'GET',
        isArray: true
    }
});

This is assuming that the string representation you receive is in valid format. I can see that , is missing after each object. Is this expected? If yes, you might want to replace } with },, wrap them in '[]' and then do angular.fromJson(data) in the transformer.

Here's a plunker that does just that: https://plnkr.co/edit/ZGLK7PTclapVNwuIlM1T?p=preview

Chanthu
  • 1,794
  • 1
  • 15
  • 22
0

You need to use angular.fromJson(). So

var WareHouseResource = $resource('/api/products?limit=10', {}, {
    query: {
        method: 'GET',
        isArray: false,
        transformResponse: function(data) {
            console.log(angular.fromJson(data));
        }
    }
});

Check out https://docs.angularjs.org/api/ng/function/angular.fromJson

var items=[{"id":"0-worfebvjyyvqjjor","size":17,"price":921,"face":"( .-.)","date":"Mon Jan 04 2016 22:55:30 GMT+0000 (GMT Standard Time)"},
{"id":"1-ifma3yxxccgzaor","size":19,"price":98,"face":"( .o.)","date":"Fri Jan 08 2016 16:11:25 GMT+0000 (GMT Standard Time)"},
{"id":"2-sa3iurvt4hv0lik9","size":14,"price":659,"face":"( `·´ )","date":"Sun Jan 03 2016 06:20:28 GMT+0000 (GMT Standard Time)"},
{"id":"3-bc3tf55q9vx11yvi","size":33,"price":361,"face":"( ° ͜ ʖ °)","date":"Fri Jan 01 2016 22:49:22 GMT+0000 (GMT Standard Time)"}]

alert(angular.fromJson(items))
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
Midhun Murali
  • 2,089
  • 6
  • 28
  • 49