我想将我的xml数据转换为json
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DataTable NameArray()
{
DataTable imageTable = new DataTable("gcm");
// imageTable.Columns.Add("image_name", typeof(String));
imageTable.Columns.Add("username", typeof(String));
imageTable.Columns.Add("gcmkey", typeof(String));
if (con.State.ToString() == "Closed")
{
con.Open();
}
string query = "SELECT username,gcmkey from gcm";
SqlCommand command = new SqlCommand(query, con);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
imageTable.Rows.Add(reader["username"],reader["gcmkey"]);
}
}
reader.Close();
con.Close();
return imageTable;
}
我正在使用此行,但它对我不起作用,请告诉我该怎么做才能转换为json
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(json.Serialize(new { PersonalProfile = reader }));
输出: -
DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="gcm" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="gcm">
<xs:complexType>
<xs:sequence>
<xs:element name="username" type="xs:string" minOccurs="0"/>
<xs:element name="gcmkey" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement xmlns="">
<gcm diffgr:id="gcm1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<username>nirav</username>
<gcmkey>12345</gcmkey>
</gcm>
</DocumentElement>
</diffgr:diffgram>
答案 0 :(得分:0)
https://stackoverflow.com/a/814027/5923666 有人已经问过,但我会给你答案......
是。为了这个目的,使用包含帮助器方法的JsonConvert类:
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// To convert JSON text contained in string json into an XML node
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
答案 1 :(得分:0)
包括来自Nuget的Newtonsoft.Json 并使用这个
JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented);
输出将是这样的:
{&#34; id&#34;:&#34; 1&#34;,&#34; name&#34;:&#34; Mr。 xyz&#34;,&#34;年龄&#34;:&#34; 25&#34;,&#34;国家&#34;:&#34;印度&#34;,&#34;地址&#34;: &#34; H no-4561&#34;,&#34;电话&#34;:&#34; 1258961&#34; },{&#34; id&#34;:&#34; 2&#34;,&#34; name&#34;:&#34; Mr。 xyz&#34;,&#34;年龄&#34;:&#34; 26&#34;,&#34;国家&#34;:&#34;印度&#34;,&#34;地址&#34;: &#34; H no-4562&#34;,&#34; Phone&#34;:&#34; 1258962&#34; },{&#34; id&#34;:&#34; 3&#34;,&#34; name&#34;:&#34; Mr。 xyz&#34;,&#34;年龄&#34;:&#34; 27&#34;,&#34;国家&#34;:&#34;印度&#34;,&#34;地址&#34;: &#34; H no-4563&#34;,&#34;电话&#34;:&#34; 1258963&#34; },{&#34; id&#34;:&#34; 4&#34;,&#34; name&#34;:&#34; Mr。 xyz&#34;,&#34;年龄&#34;:&#34; 28&#34;,&#34;国家&#34;:&#34;印度&#34;,&#34;地址&#34;: &#34; H no-4564&#34;,&#34; Phone&#34;:&#34; 1258964&#34; },{&#34; id&#34;:&#34; 5&#34;,&#34; name&#34;:&#34; Mr。 xyz&#34;,&#34;年龄&#34;:&#34; 29&#34;,&#34;国家&#34;:&#34;印度&#34;,&#34;地址&#34;: &#34; H no-4565&#34;,&#34;电话&#34;:&#34; 1258965&#34; }
让我知道它是否符合你的目的,但你觉得很难把它放在一起