使用jquery用双引号解析json响应(JavaScriptSerializer)

时间:2011-02-25 11:39:09

标签: jquery asp.net-mvc json jsonp

我在我的aspx(asp.net MVC ViewPage)中使用以下代码来获取序列化输出:

var _accountNames = '<%= ViewData["AccountNames"] != null ? new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["AccountNames"]) : null%>';

这给出了以下结果:

[{"Name":"My Saving","AccountBalance":-4.2500,"ReferenceId":2},
{"Name":"My Checking","AccountBalance":0.0000,"ReferenceId":3},
{"Name":"Undeposited Funds","AccountBalance":113.6600,"ReferenceId":2},
{"Name":"Star Account "Trust"","AccountBalance":0.0000,"ReferenceId":50}]

在上述结果中,帐户(Star Account“Trust”)中有双引号。使用eval时,它会出现"missing } after property list"错误。即使使用JSON解析也会出错。

我使用此结果将下拉选项绑定为:

 var k = JSON.parse(_accountNames.replace(/'/g, '"'));
 if (k != null && k.length > 0) {
    var options = '';
    options += '<option value="">' + "--Select Account--" + '</option>';
    for (var i = 0; i < k.length; i++) {
       options += '<option value="' + k[i].AccountNameType + '">' + k[i].Name + ' $(' + k[i].AccountBalance + ')' + '</option>';
    }
}
$("select#DepositToddl").html(options);

我如何解析结果中有双引号?请建议。

2 个答案:

答案 0 :(得分:1)

使用HttpUtility.JavaScriptStringEncode

您的服务器代码将变为:

var _accountNames = '<%= ViewData["AccountNames"] != null ? HttpUtility.JavaScriptStringEncode(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["AccountNames"])) : null%>';

答案 1 :(得分:0)

我意识到这已经过时了,但是由于克雷格没有发布一个例子......我想我会这么做。

正如Craig对你的问题的评论所指出的那样,你的JSON很糟糕。

如果你把它作为C#语句放到LINQPad中,你就可以正确地转义引号:

(你需要System.Runtime.Serialization和System.Web.Extensions。)

Accounts accounts = new Accounts();
accounts.AccountNames = new List<Account>();
accounts.AccountNames.Add(new Account() { Name = "My Saving", AccountBalance = -4.25, ReferenceId = 2});
accounts.AccountNames.Add(new Account() { Name = "My Checking", AccountBalance = 0, ReferenceId = 3});
accounts.AccountNames.Add(new Account() { Name = "Undeposited Funds", AccountBalance = 113.66, ReferenceId = 2});
accounts.AccountNames.Add(new Account() { Name = "Star Account \"Trust\"", AccountBalance = 0, ReferenceId = 50});

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
jsonSerializer.Serialize(accounts.AccountNames).Dump();
}

[System.Runtime.Serialization.DataContract]
public class Accounts {
    [System.Runtime.Serialization.DataMember]
    public List<Account> AccountNames { get; set; }
}

[System.Runtime.Serialization.DataContract]
public class Account {
    [System.Runtime.Serialization.DataMember]
    public string Name { get; set; }
    [System.Runtime.Serialization.DataMember]
    public double AccountBalance { get; set; }
    [System.Runtime.Serialization.DataMember]
    public int ReferenceId { get; set; }

返回以下JSON数据:

[{"Name":"My Saving","AccountBalance":-4.25,"ReferenceId":2},
{"Name":"My Checking","AccountBalance":0,"ReferenceId":3},
{"Name":"Undeposited Funds","AccountBalance":113.66,"ReferenceId":2},
{"Name":"Star Account \"Trust\"","AccountBalance":0,"ReferenceId":50}]

您可能已经解决了这个问题,但看看您的ViewData是如何填充的会很有趣。它只是一个动态创建的字符串,还是真的基于一个对象?