0

This is a line of json_encode function in PHP

echo $_GET['callback'].'('.json_encode($jsona).')'; 

that return

jQuery183022578700515441597_1353992348618([{"label":["done","open","pending","draft","cancel"],"values":[{"label":"August","values":["175","32","1","0","0"]},{"label":"September","values":["450","130","1","0","1"]},{"label":"October","values":["150","396","1","5","0"]},{"label":"November","values":["0","0","0","3","0"]}]}])

And I get them by JS (jQuery)

    $.ajax({
                url: 'ajax/_state.php',
                type: 'GET',
                dataType: 'jsonp',
                dataCharset: 'jsonp',
                success: function (data) {
console.log(data);
    }
});

But all the number contained in returned JSON turned into Strings (Don't know why?).

How could I convert them to Integer for example;

{
      'label': ['done', 'open', 'pending', 'draft', 'cancel'],
      'values': [
      {
        'label': 'Sept',
        'values': [20, 40, 15, 5,2]
      }, 
      {
        'label': 'Oct',
        'values': [30, 10, 45, 10]
      }, 
      {
        'label': 'Nov',
        'values': [38, 20, 35, 17]
      }
      ]
      }
3
  • 4
    If they are printed as strings, then they were strings in PHP to begin with. Where is $jsona coming from? Commented Nov 27, 2012 at 5:14
  • As loganfsmyth says. So things need fixing in PHP not javascript. Commented Nov 27, 2012 at 5:18
  • $jsona comes from here stackoverflow.com/questions/13576402/… Commented Nov 27, 2012 at 5:25

1 Answer 1

1

The values in $jsona must be strings. If you are using PHP 5.3, you can use JSON_NUMERIC_CHECK as the second argument which will cast strings as integers when appropriate, but be careful with that option.

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

1 Comment

That's wonderful answer, use this constants fix every number strings in the JSON array.

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.