开发者

Facing problem in Serializing a vb.net string to a json string

i have the following code, please let me know where m goin' wrong..

开发者_如何学编程

VB

For Each dr As DataRow In dvItems.Table.Rows
   strItems &= "'" & dr("ItemTitle") & "',"
Next
strItems = strItems.Trim(",") // before serialize strItems contains 'mouse','keyboard','led'
strItems = JsonConvert.SerializeObject(strItems) // after serialize strItems contains "'mouse','keyboad','led'"

JavaScript: Here i'm using Autocomplete.js using JQuery

function InitAutocomplete() 
{
   var Jsondata = [<%=strItems %>].sort();
   data = jQuery.parseJSON(Jsondata);
   AutoComplete_Create('<%=txtItem.ClientId %>', data);
}

while debugging in firefox with firebug data is showing null...What i'm doin' here ??

Edit : Autocomplete.js needs data in this format 'mouse','keyboard','led' Before i was doin' this without JSON, it was working fine.


jQuery.parseJSON is for parsing JSON strings. You're handing it an array. Your JavaScript code, once it gets to the client, will look something like this:

function InitAutocomplete() 
{
   var Jsondata = ["'mouse','keyboad','led'"].sort();
   data = jQuery.parseJSON(Jsondata);
   AutoComplete_Create('someid', data);
}

...which meanson Jsondata will be an array with one entry, the string 'mouse','keyboard','led'.

If I understand what you're doing, you don't need JSON at all.

VB:

strItems = ""
For Each dr As DataRow In dvItems.Table.Rows
   ' Use JsonConvert.SerializeObject to quote and escape the
   ' string; even though we're not actually using JSON, it
   ' gives us a valid, properly-escaped JavaScript string
   ' literal.
   strItems &= JsonConvert.SerializeObject(dr("ItemTitle")) & ","
Next
strItems = strItems.Trim(",")

JavaScript (with inline VB):

function InitAutocomplete() 
{
   var data = [<%=strItems %>].sort();
   AutoComplete_Create('<%=txtItem.ClientId %>', data);
}

or even just:

function InitAutocomplete() 
{
   AutoComplete_Create('<%=txtItem.ClientId %>',
                       [<%=strItems %>].sort());
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜