Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a result of an array from JSON which may has an array inside an array. How can I parse and make it a complete Javascript Array? I set an example of a sample at here

It results:

[
    {"id":"1","company":"Gaurishankar","bus_no":"JHA 12 KH 1230"},
    {"id":"2","company":"Gaurishankar","bus_no":"BA 2 KH 2270"},
    {"id":"3","company":"Gaurishankar","bus_no":"GA 1 KH 3018"}
]

It looks more like an JS array but when I try to use it, it is an object. I have gone through many Stackoverflow links but none help. Please bear in mind that some elememts later may bear array inside an array. So far my code is: start();

function start(){
  $.ajax({
    dataType: 'json',
    url:"http://mylivecanvas.com/api/get_routes.php",
    success: function(result){
        process(result);
    }   
  });   
}


function process(object){
    var obj = JSON.parse(object);
    // Got this from browsing a Stackoverflow Question 
    var arr = $.map(obj, function(el) { return el; });
    alert(arr);
}

That still returns an object, any thing I missed?

share|improve this question
2  
Can you not just use it as an object? Key -> value pairs in javascript are objects, arrays in javascript don't have keys (at least not in terms of key value pairs) –  scrowler Jul 18 '14 at 2:25
    
@scrowler Yeah Thanks, that way works fine in my PC. Is that supported in all browsers? –  user2078462 Jul 18 '14 at 2:30
    
Objects are part of javascript and are supported in all browsers that support javascript, there's nothing wrong with using them –  scrowler Jul 18 '14 at 2:32
    
@scrowler, thanks. One more thing, the Ajax Request fails over internet even though I have a good internet connection. It works well on localhost. Any ideas why? See fiddle: JSFiddle Link –  user2078462 Jul 18 '14 at 2:51
2  
@xDNP it is because of same origin policy violation –  Arun P Johny Jul 18 '14 at 2:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.