获取XML回从Web服务时,它应该是JSON [英] Getting XML back from a web service when it should be JSON
问题描述
我是小白到ASP.NET/C#。我试图使用Web服务从数据库返回的JSON对象。我得到在Firebug一个错误,我创建一个循环引用。堆栈跟踪为JSON格式。当我直接在浏览器中查看Web服务,它是为一些奇怪的原因返回有效的XML。这里是我的web服务。
[的WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共数据集帖子(){
字符串CONNSTRING = System.Web.Configuration.WebConfigurationManager.ConnectionStrings [XXX]的ConnectionString。
康涅狄格州的SqlConnection =新的SqlConnection(CONNSTRING);
字符串SQL =SELECT * FROM文章;
SqlDataAdapter的SDA =新SqlDataAdapter的(SQL,conn);在
DataSet的DS =新的DataSet();
sda.Fill(DS);
返回DS;
}
我是新的C#,所以我不知道这是正确写入。我要的是一个JSON格式的数据集。我在做正确吗?这里是调用Web服务jQuery的。
<脚本>
$(函数(){
$阿贾克斯({
键入:POST,
网址:Web服务/ MessageBoard.asmx /帖子,
数据:{},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:功能(数据){执行console.log(数据)},
失败:函数(MSG){
//警报(MSG);
}
}); });
< / SCRIPT>
你得到XML的原因是因为在的DataSet
序列化为XML。但是,您可以使用 JSON.NET 打开XML为JSON。下面是一个例子:
使用系统;
使用System.Collections.Generic;
使用System.Data这;使用Newtonsoft.Json;类节目
{
静态无效的主要(字串[] args)
{
var数据=新的DataSet();
VAR的dataTable =新的DataTable();
的for(int i = 0;我小于5;我++){dataTable.Columns.Add(的String.Format(列{0},I),typeof运算(字符串)); }
的for(int i = 0;我小于5;我++)
{
VAR瓦尔斯=新的List<对象>();
为(中间体J = 0; J&小于5; J ++){vals.Add(的String.Format(值{0},j)的); }
dataTable.LoadDataRow(vals.ToArray(),TRUE);
} dataSet.Tables.Add(dataTable的);
dataSet.AcceptChanges(); Console.WriteLine(JsonConvert.SerializeObject(数据));
}
}
I am a noob to ASP.NET/C#. I am trying to use a web service to return a JSON object from the database. I am getting an error in Firebug that I am creating a circular reference. The stack trace is in JSON format. When I view the web service directly in the browser, it is returning valid XML for some strange reason. Here is my web service.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DataSet Posts() {
string connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string sql = "SELECT * FROM Posts";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
I'm new to C# so I don't know if this is written correctly. What I want is a DataSet in JSON format. Am I doing this correctly? Here is the jQuery that is calling the web service.
<script>
$(function () {
$.ajax({
type: "POST",
url: "WebServices/MessageBoard.asmx/Posts",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) { console.log(data) },
failure: function (msg) {
//alert(msg);
}
});
});
</script>
The reason you're getting XML is because the the DataSet
serializes to XML. However, you can use JSON.NET to turn the XML into JSON. Here is an example:
using System;
using System.Collections.Generic;
using System.Data;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var dataSet = new DataSet();
var dataTable = new DataTable();
for (int i = 0; i < 5; i++) { dataTable.Columns.Add(string.Format("Column{0}", i), typeof(string)); }
for (int i = 0; i < 5; i++)
{
var vals = new List<object>();
for (int j = 0; j < 5; j++) { vals.Add(string.Format("Value {0}", j)); }
dataTable.LoadDataRow(vals.ToArray(), true);
}
dataSet.Tables.Add(dataTable);
dataSet.AcceptChanges();
Console.WriteLine(JsonConvert.SerializeObject(dataSet));
}
}
这篇关于获取XML回从Web服务时,它应该是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!