1

I have a question regarding getting a data value from a JSON array that has no key value in Angular. I've seen plenty of examples getting the value out with a key, but I have not been able to figure this piece out.

The JSON that is returned from the API is quite complicated and looks like...

[{"":796}]

Its then assigned in the controller using this..

$http({
  method: 'GET',
  url: 'api/getnumassets'
}).success(function(data) {
  $scope.numAssets = data; // response data 

});

Now when I use the snippet below to get the number out, I get the following value: {"":796} when I want it to display 796.

<div class="number">
  {{numAssets}}
</div>
<div class="desc">
  {{'viewport.totalAssets' | translate}}
</div>

I've tried a number of different accessors on the numAssets expression but I have yet to have any luck. Any help would be much appreciated!

5
  • 4
    If that is the JSON you are getting back then I would say that it's the API that needs fixing and not your code. Commented Jan 22, 2015 at 17:38
  • Perhaps so... I'm pretty new at this whole environment. The API is just doing a JSON on a select count(*) from the database Commented Jan 22, 2015 at 17:40
  • 2
    Well then depending on how it is translating the SQL output then putting select count(*) as count would probably give you a JSON output of {"count":796} which would be much better. Commented Jan 22, 2015 at 17:41
  • Derr. Exactly what I was looking for thanks. Typically I just get a count back in C++ and use it... just a bit different. :) Technically the answer below is correct and works but I prefer this method. What is the best way to answer the question? Commented Jan 22, 2015 at 17:44
  • 3
    Well Paul answered your question correctly as it is currently asked so I would accept his answer. And then just go fix your SQL and call it a day. :) Commented Jan 22, 2015 at 17:47

1 Answer 1

2

If object is obj = {"":796} you can access its property this way: obj[''].

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

1 Comment

This works and I'm marking it as the correct answer, however I preferred Matthew's method of fixing my API to return a better JSON object. Thanks!

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.