我正在通过以下代码将DataTable转换为JSON:
public string DataTableToJSONWithJSONNet(DataTable table)
{
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(table);
return JSONString;
}
问题似乎是这没有返回正确的JSON。 JSONString的值为:
"{\"DataTable.RemotingVersion\":{\"Major\":2,\"Minor\":0,\"Build\":-1,\"Revision\":-1,\"MajorRevision\":-1,\"MinorRevision\":-1},\"XmlSchema\":\"<?xml version=\\\"1.0\\\" encoding=\\\"utf-16\\\"?>\\r\\n<xs:schema xmlns=\\\"\\\" xmlns:xs=\\\"http://www.w3.org/2001/XMLSchema\\\" xmlns:msdata=\\\"urn:schemas-microsoft-com:xml-msdata\\\">\\r\\n <xs:element name=\\\"Table1\\\">\\r\\n <xs:complexType>\\r\\n <xs:sequence>\\r\\n <xs:element name=\\\"ID\\\" type=\\\"xs:int\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"ImageURL\\\" type=\\\"xs:string\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"Price\\\" type=\\\"xs:decimal\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"Description\\\" type=\\\"xs:string\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"UploadDate\\\" type=\\\"xs:dateTime\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"ExpDate\\\" type=\\\"xs:dateTime\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n <xs:element name=\\\"Status\\\" type=\\\"xs:string\\\" msdata:targetNamespace=\\\"\\\" minOccurs=\\\"0\\\" />\\r\\n </xs:sequence>\\r\\n </xs:complexType>\\r\\n </xs:element>\\r\\n <xs:element name=\\\"tmpDataSet\\\" msdata:IsDataSet=\\\"true\\\" msdata:MainDataTable=\\\"Table1\\\" msdata:UseCurrentLocale=\\\"true\\\">\\r\\n <xs:complexType>\\r\\n <xs:choice minOccurs=\\\"0\\\" maxOccurs=\\\"unbounded\\\" />\\r\\n </xs:complexType>\\r\\n </xs:element>\\r\\n</xs:schema>\",\"XmlDiffGram\":\"<diffgr:diffgram xmlns:msdata=\\\"urn:schemas-microsoft-com:xml-msdata\\\" xmlns:diffgr=\\\"urn:schemas-microsoft-com:xml-diffgram-v1\\\">\\r\\n <tmpDataSet>\\r\\n <Table1 diffgr:id=\\\"Table11\\\" msdata:rowOrder=\\\"0\\\">\\r\\n <ID>1</ID>\\r\\n <ImageURL>https://i.imgur.com/S59HjAe.jpg </ImageURL>\\r\\n <Price>123.0000</Price>\\r\\n <Description>123</Description>\\r\\n <UploadDate>2019-04-19T08:30:56.913-05:00</UploadDate>\\r\\n <ExpDate>1900-01-01T00:00:00-06:00</ExpDate>\\r\\n <Status>New</Status>\\r\\n </Table1>\\r\\n </tmpDataSet>\\r\\n</diffgr:diffgram>\"}"
据我了解,结果不应包含该字符串开头的大部分内容(修订,XML Schema,列的数据类型等)。
任何人都可以解释为什么在转换DT后我还能获得更多信息吗?我相信其他信息会导致“位置0的json中出现意外的令牌u”错误。感谢您的帮助。
编辑:利用此功能的javascript代码是:
function GetItemsList() {
try {
$('#Products').html('');
var items = '';
document.getElementById("Products").innerHTML = ""
var i = 0;
$.ajax({
type: "GET",
url: '/Home/UseGetItems',
contentType: "application/json; charset=utf-8",
success: function (result) {
var ItemData = JSON.parse(result.Data);
if (ItemData.length > 0) {
for (i; i < ItemData.length; i++) {
document.getElementById("Products").innerHTML += '<div class="w3 - col l3 s6"><div class="w3 - container"><img src="' + ItemData[i].ImageURL + '" style="width:100%"><p>' + ItemData[i].Description + '<br><b>' + ItemData[i].Price + '</b></p></div></div>';
}
$('#Products').append(items);
}
},
});
} catch (e) {
}
}
编辑2:其余的C#是:
public ActionResult UseGetItems()
{
jsonMessage result = new jsonMessage();
var ItemsList = GetItems();
return Json(new
{
Data = ItemsList,
result = true,
});
}
public string GetItems()
{
var Items = string.Empty;
try
{
DataTable dt = new DataTable();
string Connection = "Data Source=TEST; Initial Catalog=TEST;Persist Security Info=True;User ID=TEST;Password=TEST"; //ConfigurationManager.ConnectionStrings["testConn"].ConnectionString;
string Query = "SELECT * FROM [TEST] WHERE [Status] <> 'CLOSED'";
using (SqlConnection conn = new SqlConnection(Connection))
{
conn.Open();
SqlCommand cmd = new SqlCommand(Query, conn)
{
CommandType = CommandType.Text
};
//cmd.Parameters.Add(new SqlParameter("@this", "value"));
cmd.ExecuteNonQuery();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dt);
conn.Close();
};
Items = DataTableToJSONWithJSONNet(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return Items;
}
@briosheje console.log(result);
的结果是
Object
data: "{"DataTable.RemotingVersion":{"Major":2,"Minor":0,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"XmlSchema":"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n <xs:element name=\"Table1\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"ID\" type=\"xs:int\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"ImageURL\" type=\"xs:string\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"Price\" type=\"xs:decimal\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"Description\" type=\"xs:string\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"UploadDate\" type=\"xs:dateTime\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"ExpDate\" type=\"xs:dateTime\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n <xs:element name=\"Status\" type=\"xs:string\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n <xs:element name=\"tmpDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n <xs:complexType>\r\n <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\" />\r\n </xs:complexType>\r\n </xs:element>\r\n</xs:schema>","XmlDiffGram":"<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">\r\n <tmpDataSet>\r\n <Table1 diffgr:id=\"Table11\" msdata:rowOrder=\"0\">\r\n <ID>1</ID>\r\n <ImageURL>https://i.imgur.com/S59HjAe.jpg </ImageURL>\r\n <Price>123.0000</Price>\r\n <Description>123</Description>\r\n <UploadDate>2019-04-19T08:30:56.913-05:00</UploadDate>\r\n <ExpDate>1900-01-01T00:00:00-06:00</ExpDate>\r\n <Status>New</Status>\r\n </Table1>\r\n </tmpDataSet>\r\n</diffgr:diffgram>"}"
result: true
__proto__: Object
编辑3:如果我将C#更改为要返回的DT(而不是在那里进行序列化),则如下所示:
public DataTable GetItems()
{
DataTable Items;
try
{
DataTable dt = new DataTable();
string Connection = "Data Source=TEST; Initial Catalog=TEST;Persist Security Info=True;User ID=TEST;Password=TEST"; //ConfigurationManager.ConnectionStrings["testConn"].ConnectionString;
string Query = "SELECT * FROM [TEST] WHERE [Status] <> 'CLOSED'";
using (SqlConnection conn = new SqlConnection(Connection))
{
conn.Open();
SqlCommand cmd = new SqlCommand(Query, conn)
{
CommandType = CommandType.Text
};
//cmd.Parameters.Add(new SqlParameter("@this", "value"));
cmd.ExecuteNonQuery();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dt);
conn.Close();
};
Items = dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return Items;
}
然后var ItemData = JSON.parse(result.data);
的结果给出错误:
Unexpected token o in JSON at position 1
编辑4:
public class jsonMessage
{
public string Message { get; set; }
public string MessageList { get; set; }
public bool Status { get; set; }
}