如何使用 Prototype(JS) 评估这个特定的 JSON?

发布于 2024-10-31 08:04:36 字数 514 浏览 0 评论 0原文

来自服务器的数据变成了 JSON 数组,作为 requestText 和 ajax 请求:

Price : [{"id":"1","max_price":"100000"},{"id":"2","max_price":"150000"},{"id":"3","max_price":"200000.55"}]

Name : [{"id":"1","name":"P1"},{"id":"2","name":"P2"},{"id":"3","name":"P3"}]

我看到了这个方法的原型: var json = Transport.responseText.evalJSON(true);

我怎样才能获取 Price 数组,所以JSON 等于:

[{"id":"1","max_price":"100000"},{"id":"2","max_price":"150000"},{"id":"3","max_price":"200000.55"}]`

Data from a Server became a JSON array as responseText an ajax request :

Price : [{"id":"1","max_price":"100000"},{"id":"2","max_price":"150000"},{"id":"3","max_price":"200000.55"}]

Name : [{"id":"1","name":"P1"},{"id":"2","name":"P2"},{"id":"3","name":"P3"}]

I see into Prototype this method : var json = transport.responseText.evalJSON(true);

How can I just get the Price array, so JSON equals:

[{"id":"1","max_price":"100000"},{"id":"2","max_price":"150000"},{"id":"3","max_price":"200000.55"}]`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-11-07 08:04:36

php 需要包含 json 标头:

header('Content-type: application/json');

ajax 请求需要包含 evalScripts (确保您信任源):

new Ajax.Request("json.php",
          { method: 'get',
            parameters: {'xyz': 'json', 'var2': 'some_val'},
            evalScripts: true,
            onSuccess: function(response){your_function(response);}});

然后您的函数可以像下面这样获取 json:

your_function = function (response) {
  var result = response.responseJSON;
  ...
}

编辑: 还有直接来自源的这些说明:
http://www.prototypejs.org/learn/json


Edit2:以下是更新服务器的方法:

$return_data = array();
$return_data['price'] = getPrice($db); 
$return_data['name'] = getName($db);
echo json_encode($return_data)."\n";

执行此操作后,在 js 中您可以执行类似以下操作(来自上面的 your_function 示例):

alert ("first id is: " + result['price'][0]['id']);

The php needs to include the json header:

header('Content-type: application/json');

The ajax request needs to include evalScripts (make sure you trust the source):

new Ajax.Request("json.php",
          { method: 'get',
            parameters: {'xyz': 'json', 'var2': 'some_val'},
            evalScripts: true,
            onSuccess: function(response){your_function(response);}});

Then your function can get the json like the following:

your_function = function (response) {
  var result = response.responseJSON;
  ...
}

Edit: There's also these instructions directly from the source:
http://www.prototypejs.org/learn/json


Edit2: Here's how to update the server:

$return_data = array();
$return_data['price'] = getPrice($db); 
$return_data['name'] = getName($db);
echo json_encode($return_data)."\n";

after you do this, in js you can do something like the following (from the above your_function example):

alert ("first id is: " + result['price'][0]['id']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文