0

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.

1 Answer 1

1

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());
}
Sign up to request clarification or add additional context in comments.

6 Comments

JsonConvert.SerializeObject(strItems) is not converting to JSON string ??
@Crowder: i agree, but if items contains this '] then my script is ending, thats why i'm trying to use JSON
@FosterZ: Right, you need to be sure that any strings you output are properly escaped (since you're outputting strings using ' as the string delimiter, you have to ensure any ' in the string has a backslash in front of it; you also have to be sure any backslash has a backslash in front of it). I can't find a reference page for JsonConvert.SerializeObject (surprisingly), but from the quotes in your question, it may well be able to help -- see the comment in the code above.
for this JsonConvert.SerializeObject i'm using Newtonsoft.Json and i guess i have to find sm other solution for escaping '] coz i dn't think so that this JsonConvert.SerializeObject will help..
@FosterZ: Well, as far as I can tell, JsonConvert.SerializeObject(string) should work, since according to the comments in the code in your question, if you pass a string into it, it's properly quoting the string (using double quotes instead of singles, but that's fine). Let me update the answer to show you what I mean.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.