2

I need to call a web service from javascript that returns a JSON array in the following format:

["hello","there","I","am","an","array"]

Unfortunately, the javascript library I'm using (Sencha Touch) to load this data into a widget doesn't accept that format as data input. It will work with something like this however:

[["hello"],["there"],["I"],["am"],["an"],["array"]]

So there are two questions here-- how can I call that web service in javascript and then manipulate the returned array into the format I need? I've been looking at jQuery's getJson() method, not sure if that's the way to go or if there are better ways.

The URL that delivers the JSON array is this right here. Thanks for your help.

2 Answers 2

2

Here's a jsFiddle showing both parts of what you asked about. I'm using jQuery for my AJAX call (faked in the jsFiddle) and I'm using Underscore.js for the manipulation:

http://jsfiddle.net/JohnMunsch/E7YTQ/

// Part 1: Get the raw data. Unfortunately, within a jsFiddle I can't go get it from a different domain. So I'm
// simulating the call instead.
var rawDataPromise = $.ajax({
    url : "/echo/json/",
    data : fakeData,
    type : "POST"
});
// var rawDataPromise = $.ajax("http://fastmotorcycleservice.cloudapp.net/FastMotorCycleListService.svc/list/Bruno");

rawDataPromise.done(
  function (results) {
    // Part 2: Manipulate the data into the form we need.
    var manipulatedResults = _.map(results, function (item) { return [ item ]; });

    console.log(manipulatedResults);
  }
);

// You could also pull that together into a single call $.ajax(...).done(function (results) { ... });
0
1

once you have the data in one of your local variables you can manipulate it as you want:

var data = the_function_you_use_to_get_data();
var formatted_array = new Array();
for(i in data){
    var d = new Array();
    d[0] = i;
    formatted_array.push(d);
}

i hope it answered your question

0

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.