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

时间:2022-09-15 13:32:51

I am using the following code in my aspx(asp.net MVC ViewPage) to get serialized output:

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

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

This gives the following result:

这就产生了以下结果:

[{"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}]

In the above result, the Account (Star Account "Trust") has double quotes in it. It gives "missing } after property list" error, while using eval. Even using JSON parse gives error.

在上面的结果中,账户(明星账户“Trust”)有双引号。在使用eval时,它会出现“属性列表后缺少}”的错误。即使使用JSON解析也会出错。

I am using this result to bind the dropdown options as:

我正在使用这个结果来绑定下拉选项,如下所示:

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

How can i parse the result has double quotes in it? Please suggest.

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

2 个解决方案

#1


1  

Use HttpUtility.JavaScriptStringEncode.

使用HttpUtility.JavaScriptStringEncode。

Your server tag will become:

您的服务器标记将变成:

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

#2


0  

I realize this is old, but since Craig didn't post an example ... I figured I would.

我知道这已经过时了,但是因为克雷格没有发布一个例子……我想我会的。

As noted by Craig's comment to your question, your JSON is bad.

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

If you drop this into LINQPad as a C# statement you'll get the quotes escaped correctly:

如果你把它放到LINQPad中作为c#语句,你会得到正确的引号:

(You'll need System.Runtime.Serialization and System.Web.Extensions.)

(你需要System.Runtime。序列化和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; }

That returns the following JSON data:

返回以下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}]

You might have solved this already, but it would be interesting to see how your ViewData is populated. Is it just a string created on the fly, or is it really based on an object?

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

#1


1  

Use HttpUtility.JavaScriptStringEncode.

使用HttpUtility.JavaScriptStringEncode。

Your server tag will become:

您的服务器标记将变成:

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

#2


0  

I realize this is old, but since Craig didn't post an example ... I figured I would.

我知道这已经过时了,但是因为克雷格没有发布一个例子……我想我会的。

As noted by Craig's comment to your question, your JSON is bad.

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

If you drop this into LINQPad as a C# statement you'll get the quotes escaped correctly:

如果你把它放到LINQPad中作为c#语句,你会得到正确的引号:

(You'll need System.Runtime.Serialization and System.Web.Extensions.)

(你需要System.Runtime。序列化和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; }

That returns the following JSON data:

返回以下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}]

You might have solved this already, but it would be interesting to see how your ViewData is populated. Is it just a string created on the fly, or is it really based on an object?

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