0

I am creating a service which should read an array of arrays using AngularJS with $resource. I see my service is returning a nice JSON String like :

[["Year-Month","e","i"],["2015-Mar",133.442,124.379],["2015-Apr",163.057,126.804],["2015-May",170.029,112.344],["2015-Jun",170.939,125.220],["2015-Jul",177.132,133.504],["2015-Aug",176.723,117.596]]

Unfortunately when I read it with:

    var tResource = $resource("/t/CH/6X", {}, { get: {method:  'GET', cache: true, 
isArray: true}});
    tResource.get({}, function(data){
        ...
    });

It is simply an array of arrays.

Unfortunately the data received is not the array that was received. It is I guess, a Resource object containing all rows in my matrix. Each row have the key representing the row number and having as value another Resource object, which contains each column stored again as key/value, where the key is the column number.

In other words I would like to receive as data in my success function simply the 2 dimensional arrays sent by the server.

How can I do that?

BuckBazooka
  • 881
  • 1
  • 10
  • 18

2 Answers2

1

You could just wrap the array into JSON object: {arr: [["Year-Month","e","i"],["2015-Mar",133.442,124.379],["2015-Apr",163.057,126.804],["2015-May",170.029,112.344],["2015-Jun",170.939,125.220],["2015-Jul",177.132,133.504],["2015-Aug",176.723,117.596]]}

and then address the result.

If I remember correctly, JSON specification actually does not allow topmost arrays other than arrays of objects.

Constantine Poltyrev
  • 1,015
  • 10
  • 12
  • I am not aware of any such JSON specification prohibition. Could you provide a reference? – Garret Wilson May 11 '18 at 14:59
  • I was wrong about the specification. It actually does allow an array as topmost element. However, there are some considerations regarding arrays as topmost elements. See this discussion for more details: https://stackoverflow.com/questions/3833299/can-an-array-be-top-level-json-text – Constantine Poltyrev May 12 '18 at 20:58
0

Unfortunately AngularJS $resource doesn't seem to handle this use case. It either supports a single object (a "resource", which will be filled with data after the request finishes) or an array of objects (each of which will be filled with a "resource" after the request succeeds).

The solution is to use the lower-level $http object, like this. (Note that I'm using ES6 syntax, with arrow functions and such.)

const promise = $http.get("/t/CH/6X").then(httpResponse => httpResponse.data);

Unlike $resource, which will return an object that will be automatically filled later, this returns a promise that, when fulfilled, will provide the JSON you requested. (Note the then(httpResponse => httpResponse.data); without this, the statement would return a promise to the $http object itself.)

For more about the limitations of $resource, see Tricky behavior of AngularJS $resource service. For caching and such, see the $http documentation from the other link.

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272