我正在使用LINQ to SQL在C#/ ASP.net中创建WebService Json Parser。它正在运行,但我的 JSON 返回如下。
<string>[{"cli_id":1,"nome":"Joe"},{"cli_id":2,"nome":"Gary"},{"cli_id":3,"nome":"Ash"},{"cli_id":4,"nome":"Pedro"},{"cli_id":5,"nome":"Marcos"}]</string>
我不想要字符串标签。
我正在尝试此代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
}
DataClassesDataContext dados = new DataClassesDataContext();
[WebMethod]
public string getCustomers() {
var json = "";
var clientes = from result in dados.clientes select result;
JavaScriptSerializer jss = new JavaScriptSerializer();
json = jss.Serialize(clientes);
return json;
}
}
它返回 JSON ,但使用XML和<string> </string>
如何删除XML?
答案 0 :(得分:1)
我将代码更改为:
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public void getCustomers() {
// var json = "";
Context.Response.Clear();
Context.Response.ContentType = "application/json";
var clientes = from result in dados.clientes select result;
JavaScriptSerializer jss = new JavaScriptSerializer();
Context.Response.Write(jss.Serialize(clientes));
}
答案 1 :(得分:1)
pkg-config --cflags --libs /usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
现在,当您创建了Web方法时,请使用此ajax调用从Web服务中获取数据
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void getCustomers()
{
var clientes = from result in dados.clientes select result;
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Write(JsonConvert.SerializeObject(clientes));
}
答案 2 :(得分:1)
试试这段代码。安装Newtonsoft.Json包。
<强> CODE 强>
public class HelloWorldData
{
public String Message;
}
[WebMethod]
public void getCustomers()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
HelloWorldData data = new HelloWorldData();
string sql = "SQL_QUERTY";
SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.AppSettings["CONNECTION_STRING"]);
DataSet ds = new DataSet();
da.Fill(ds);
Context.Response.Write(JsonConvert.SerializeObject(ds, Newtonsoft.Json.Formatting.Indented));
}