【问题标题】:jQuery cannot deserialize json objectjQuery 无法反序列化 json 对象
【发布时间】:2016-06-13 04:29:18
【问题描述】:

我有这个回报来自这里

 public string Get(int id)
        {
            return "{\"longPachage\":{\"Id\":0}}";
        }

我通过 ajax 收到这个返回的代码

 $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",           
            url: "http://localhost:3148/api/values/5",           
            success: function (data) {
                alert(data);                
                alert(" Success ");
            },
            error: function (data) {

                alert(" Error ");
            }
        })

如何反序列化 json 对象并仅打印 Id 值?

【问题讨论】:

  • 试试data.longPachage.Id
  • 不要试图像那样返回json。而是返回对象的JsonResult(您可以删除无意义的contentType: "application/json; charset=utf-8", 选项)

标签: c# jquery json ajax asp.net-mvc


【解决方案1】:

您可以使用此代码。它可能对你有帮助。 您没有解析数据以获取字符串格式的 JSON。因此,您现在可以使用此代码获取字符串格式的 JSON 数据。

var obj = JSON.parse(data);


$.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",           
        url: "http://localhost:3148/api/values/5",           
        success: function (data) {
        var obj = JSON.parse(data);
            alert(obj.Id);                
            alert(" Success ");
        },
        error: function (data) {

            alert(" Error ");
        }
    })

【讨论】:

  • 警报“未定义”
  • 你能提醒 obj 变量查看字符串中的内容吗?
  • 警报 [object 对象]
【解决方案2】:

是的,斯蒂芬是对的。

你必须从控制器发送一个 json 结果。

例如。

public JsonResult Get(int id)
{
    return Json(new 
    { 
          longPachage = new { ID = 0 } 
    }, JsonRequestBehavior.AllowGet);
}

在您的 ajax 成功时,只需检索该对象或 dataID。

$.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",           
        url: "http://localhost:3148/api/values/5",           
        success: function (data) {
            var dataID = data.longPachage.Id;
            // Do with Your ID.
        },
        error: function (data) {
            //Do anything for error here.
        }
    })

【讨论】:

  • 还有什么问题?只需将调试器放入成功函数中,然后查看您的 dataID 是否有任何值。
【解决方案3】:

像这样改变你的方法:

public ActionResult Get(int? id)
{
    return Content("{\"longPachage\":{\"Id\":0}}");
}

然后在你的 jQuery 中:

$.getJSON("http://localhost:3148/api/values", {id:5}, function(data) {
    var id = data.longPachage.Id;
    alert(id)
});

【讨论】:

  • JsonResult 不会返回字符串 :))
  • 要返回json,它的return Json(new { longPachage = new { ID = 0 } }, JsonRequestBehavior.AllowGet);
  • 我听不懂你
  • @OsamaElsayed 哪一部分你不能?
  • 这部分 public JsonResult Get(int id) { return Json(new { longPachage = new { ID = 0 } }, JsonRequestBehavior.AllowGet); } 我不想改变我想要反序列化这个返回的返回 {\"longPachage\":{\"Id\":0}}
【解决方案4】:

尝试在 MVC 中使用 JsonResult 来返回 Json 而不是构建字符串。 JsonResult 只是 ActionResult 的一个抽象类。修改控制器中的代码如下

方法一:

public JsonResult GetTest(int id)
{
    return this.Json(new { ID = 0 }, JsonRequestBehavior.AllowGet);
}

方法二:

尝试创建一个模型类为 LongPachage

public class LongPachage()
{
   public int ID {get;set;}
}

并尝试如下调用控制器方法

public JsonResult Get(int id)
    {
        LongPachage model = new LongPachage();
        model.ID = 0;
        return this.Json(model, JsonRequestBehavior.AllowGet);
    }

方法3

创建模型类说 TestModel 并尝试添加 LongPachage 类作为该类的属性。

public class TestModel()
    {
       public LongPachage LongPachage {get;set;}
    }

并尝试如下调用控制器方法

public JsonResult Get(int id)
    {
        TestModel model = new TestModel();
        model.LongPachage .ID = 0;
        return this.Json(model, JsonRequestBehavior.AllowGet);
    }

然后在视图中使用 AJAX GET wrapper 尝试如下实现

 $.get('@Url.Action("Get","ControllerName")', { id: "5" })
                .done(function (data) {
                   // If you are using Method 1 or Method 2 alert will be as follows
                    alert(data.ID);
                   // If you are using method 3 
                    alert(data.LongPachage.ID)
                });

【讨论】:

  • 返回此错误当前上下文中不存在名称'Json'
  • 你能告诉我你用的是哪种方法
  • 你的控制器中是否包含了所有足够的命名空间
猜你喜欢
  • 2016-12-30
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2017-03-16
相关资源
最近更新 更多