2013-01-25 5 views
0

json 데이터에 대한 질문이 있습니다. asp.net 웹 서비스에서 데이터베이스의 데이터를 json 데이터로 변환 할 수 있지만 XML 태그가 포함되어 있습니다. 이 데이터에서 문자열 태그와 xml 정보를 제거해야합니다. 데이터의 모양은 다음과 같습니다json 데이터에서 xml 태그 제거

?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}] 
+1

은 [이] 한 번 봐 (http://stackoverflow.com/questions/9288270/webservice-returns-json-data- 되세요 xml-header-how-can-we-remove) 질문에 도움이 될 수도 있습니다. –

답변

0

당신은 당신이 JSON을 (가로장 설치 등등이 될 수있는) 반환 할 데이터와 힘 ASP.NET을 요청하는 방법에 대해 살펴해야합니다.

장식 웹 서비스 방법 : 다음

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public List<string> GetData() { 

콘텐츠 유형을 설정해야합니다 및 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 
    } 
}); 
+0

내 코드가 아래에 있습니다. 스크립트 메서드에 문자열을 사용했습니다. 문제가 무엇인지 이해할 수 없습니다. – pln

+0

문자열을 반환하기 때문에 XML이 반환됩니다. JSON을 요청하고 .NET이 객체의 직렬화를 처리하도록해야합니다. – Echilon

+0

감사합니다 Echilon! – pln

1

수입 System.Data

수입 시스템 .Data.SqlClient

수입 System.Collections.Generic

수입 System.Web.Script.Serialization

Public 클래스 제품

Public ProductID As Integer 
    Public ProductName As String 
    Public VendorID As Integer 
    Public GroupID As Integer 

최종 클래스

공공 기능 GetProductJSon() 문자열로

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 

최종 기능

<WebMethod()> _ 

<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _ 

공공 하위 GetProduct()

Dim str As String = GetProductJSon() 
    Context.Response.Clear() 
    Context.Response.ContentType = "text/json" 
    Context.Response.Charset = "utf-8" 
    Context.Response.Write(str) 

최종 하위