I am trying to return a json string via jQuery of an object using the following function. The problem I do not seem to be able to overcome is my json result comes out the other end wrapped in double quotes.
I have seen in this post that I should;
have your method return an actual object and let the JSON serialization of the framework do the heavy lifting for you
But I don’t fully understand that this means.
This is the function I am using.
Public Function getLine() As String
Dim data As List(Of ArrayList) = New List(Of ArrayList)
For Each q In getAllData()
Dim a As New ArrayList
Dim d As Date = q.DateTime
a.Add(d.Ticks)
a.Add(q.TotalRevenue)
data.Add(a)
Next
Dim s As New Serie
s.data = data
Dim str As String = JsonConvert.SerializeObject(s)
Return str
End Function
Any ideas greatly appreciated.
$.ajax({
type: "POST",
dataType: "json",
data: "{}",
contentType: "application/json; charset=utf-8",
url: "_services/ScriptService.asmx/getData",
success: function (items) {
var data = eval("(" + items.d + ")");
},
cache: false,
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
});
‘items.d’ contains…
"{"data":[[634420512000000000,100000.0000] ... [634421376000000000,100086.0000]]}"
if i eval ‘items.d’ i get…
[[634420512000000000, 100000] ... [634421376000000000, 100086]
I read somewhere that eval is evil, is this true?
Not clear whether you’re having problem with the code you posted or with the javascript that handles it. So you’re getting a result, it’s just wrapped in double quotes? If you put a breakpoint at the beginning of the javascript callback function in Visual Studio, you should be able to see what is being returned. Just call eval on that string, and extract out whatever you need from the object. From that other article you quoted: