Reputation: 143
I have question about json data. I can convert data from database to json data in asp.net web service but they come with xml tags. I need to remove string tags and xml information from this data. The appearance of the data is :
?xml version="1.0" encoding="utf-8" ?
string xmlns="http://tempuri.org/"
[{"ID":10,"Duration":24235},{"ID":21,"Duration":9034},{"ID":12,"Duration":13681},{"ID":1,"Duration":23053},{"ID":13,"Duration":22863},{"ID":22,"Duration":57163}]
Upvotes: 0
Views: 1750
Reputation: 11
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
Public Class Product
Public ProductID As Integer
Public ProductName As String
Public VendorID As Integer
Public GroupID As Integer
End Class
Public Function GetProductJSon() As String
Dim ls As New List(Of Product)
Dim Temp As String = ""
Dim js As New JavaScriptSerializer
Dim json As String
Try
Using cn As New SqlConnection(Helper.ConnectionString)
Using cm As SqlCommand = cn.CreateCommand()
cm.CommandType = CommandType.StoredProcedure
cm.CommandText = "GetProdct"
cm.Connection.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cm)
Dim dt As DataTable = New DataTable
Dim ds As DataSet = New DataSet
da.Fill(ds, "Product")
Dim clsProduct As New Product
dt = ds.Tables(0)
For i = 0 To dt.Rows.Count - 1
clsProduct.ProductID = dt.Rows(i)("ProductID")
clsProduct.ProductName = dt.Rows(i)("ProductName")
clsProduct.VendorID = dt.Rows(i)("VendorID")
clsProduct.GropID = dt.Rows(i)("GropID")
ls.Add(clsProduct)
Next
End Using
End Using
json = js.Serialize(ls)
Return json
Catch ex As Exception
End Try
End Function
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
Public Sub GetProduct()
Dim str As String = GetProductJSon()
Context.Response.Clear()
Context.Response.ContentType = "text/json"
Context.Response.Charset = "utf-8"
Context.Response.Write(str)
End Sub
Upvotes: 1
Reputation: 10264
You need to look at how you're requesting the data and force ASP.NET to return JSON (which can be fiddly).
Decorate your web service method with:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<string> GetData() {
Then make sure to set the content type and request the data via POST:
$.ajax({
type: "POST",
url: "/YourService.asmx/GetData",
data: markers,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
// your actual data will be in data.d
},
failure: function(errMsg) {
// show error
}
});
Upvotes: 0