【问题标题】:How to access values in JSON object? [duplicate]如何访问 JSON 对象中的值? [复制]
【发布时间】:2014-07-16 02:31:27
【问题描述】:

status 是可用的 JSON 对象吗?

如何访问status 上的action_successnewIndex 的值?

服务器:

[HttpPost]
public ActionResult UploadFiles()
{
    // save file..
    return Json(new { action_success = "true", newIndex = 2 });
}

客户:

complete: function (e, data) {
    var status = e.currentTarget.response;
    // From FireBug: status is "{"action_success":"true","newIndex":2}"
    // Including the first and last double-quote.

    // I want to do something like:
    // status.action_success and status.newIndex but I can't!    
}

编辑: 简单的解决方案:

var statusParsed = JSON.parse(status);
var success = statusParsed.action_success;
var index = statusParsed.newIndex;

【问题讨论】:

  • 嗯我需要 JSON.parse()?
  • 我在写这个问题时并不知道 JSON.parse()。我是我后来找到的东西。我现在正在尝试,不要恨我。
  • JavaScript中使用了哪些AJAX API,它是如何发起的? (jQuery 通常会默认自动解析 JSON。)
  • 我可以通过 "." 访问 action_success 和 newIndex现在从状态开始,在添加此行之后:` var statusParsed = JSON.parse(status); `
  • @user2864740 我不明白你的意思。我可以告诉你我的 jQuery 版本和 wijmo 的版本,如果是这样的话?这是您在我的客户端看到的 wijUpload 函数。

标签: javascript ajax json response


【解决方案1】:

我认为你可以直接使用 JsonResult 并且应该可以工作

[HttpPost]
public JsonResult UploadFiles()
{
// save file..
return Json(new { action_success = "true", newIndex = 2 });
}

如果您使用的是 $.ajax() 在客户端,则将数据类型设置为 json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多