开发者

RegisterArrayDeclaration to JSON Array

How do RegisterArrayDeclaration as a JSON Object collection like this.

var data = [
            {Color:'#fff开发者_如何学编程fff', Image:'images/image1.jpg'},
            {Color:'#ff9900', Image:'images/image2.jpg'}
           ];

instead of var myArray = new Array("", "")


That is not JSON, that is a literal array containing literal objects. JSON is a text format based on Javascript syntax, but that code could not be used as JSON as it's outside of the subset of the syntax that JSON uses.

If you want the declaration to look exactly like that (although there is no need for that), you would use the RegisterClientScriptBlock method instead:

StringBuilder script = new StringBuilder();
script.AppendLine("var data = [");
script.AppendLine("            {Color:'#ffffff', Image:'images/image1.jpg'},");
script.AppendLine("            {Color:'#ff9900', Image:'images/image2.jpg'}");
script.AppendLine("           ];");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "data", script.ToString(), true);

The JSON string describing that data would look like this:

[
  {"Color":"#ffffff","Image":"images/image1.jpg"},
  {"Color":"#ff9900","Image":"images/image2.jpg"}
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜