2

次の形式でJSON配列を返すjavascriptからWebサービスを呼び出す必要があります。

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

残念ながら、このデータをウィジェットにロードするために使用しているjavascriptライブラリ(Sencha Touch)は、その形式をデータ入力として受け入れません。ただし、次のようなもので動作します。

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

したがって、ここには2つの質問があります。JavaScriptでそのWebサービスを呼び出して、返された配列を必要な形式に操作するにはどうすればよいですか。私はjQueryのgetJson()メソッドを見てきましたが、それが進むべき道なのか、それとももっと良い方法があるのか​​わかりません。

JSON配列を配信するURLは ここにあります。 ご協力いただきありがとうございます。

4

2 に答える 2

2

これは、あなたが質問したことの両方の部分を示すjsFiddleです。私はAJAX呼び出し(jsFiddleで偽造)にjQueryを使用しており、操作にはUnderscore.jsを使用しています。

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) { ... });
于 2012-04-13T16:30:02.890 に答える
1

ローカル変数の1つにデータが入ったら、必要に応じて操作できます。

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);
}

私はそれがあなたの質問に答えたことを願っています

于 2012-04-13T16:13:36.700 に答える