且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将xml转换为json

更新时间:2023-02-14 10:07:46

.ajax({
type: POST,
contentType: application / json ; charset = utf-8
url:' url'
数据: {}
async: false
dataType: xml
成功:功能(数据){
build_menu(data)
},



我从webmethod返回的xml数据,如

 <   NewDataSet  >  
< >
< UserID > 17727 < / UserID >
< 用户 > 测试用户< /用户 >
< 订购 / >
< /表 >
< 表1 >
< id > 21 < / id >
< CustomerID > 12872 < / CustomerID >
< 客户 > Actavis Group < / Customer >
< Primarycsrid > 16231 < / Primarycsrid >
< PrimaryCSR > Kavitha Rani Aryamarati < / PrimaryCSR >
< ImplicitSameBranchAccess > true < / ImplicitSameBranchAccess >
< azGroup > A - C < / azGroup >
< IsLoggedOnUserCustomer > false < / IsLoggedOnUserCustomer >
< / Table1 >
< / NewDataSet >



i想要显示我的输出as

 {
NewDataSet:{
Table:[
{
UserID:17727,
用户:测试用户
},],
表1:[
{
id:21,
CustomerID: 12872,
客户:Actavis集团,
Primarycsrid:16231,
PrimaryCSR:Kavitha Rani Aryamarati,
ImplicitSameBranchAccess: true,
azGroup:A - C,
IsLoggedOnUserCustomer:false
},]
}
}



怎样toonvert xml到json请帮帮我kquote>

请检查以下链接

您可以使用nuget包 newtonsoft json [ ^ ]



并轻松将任何集合或对象转换为json

作为通用集合,数据集,数据表,

从xml生成数据表并将其转换为json。 />


JSON [ ^ ]





ok



然后你可以使用

这里dt是DataTable

 System.Web.Script.Serialization.JavaScriptSerializer序列化器=  new  System.Web.Script.S erialization.JavaScriptSerializer(); 
List< Dictionary< string,object>> rows = new List< Dictionary< string,object>>();
Dictionary< string,object>行;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary< string,object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName,dr [col]);
}
rows.Add(row);
}
返回 serializer.Serialize(rows);





使用此方法

  private  StringBuilder DatasetToJson(DataSet ds)
{
var sb = new StringBuilder();
var serializer =
new JavaScriptSerializer();
var rows = new List< Dictionary< string,object>>();
Dictionary< string,object>行;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
row = dt。 Columns.Cast< DataColumn>()。ToDictionary(col = > col.ColumnName,col = > dr [col]);
rows.Add(row);
}
sb.Append(serializer.Serialize(rows));
}
return sb;
}


in web method iam returning xml string like

MemoryStream ms = new MemoryStream();
      dsCustomer.WriteXml(ms, XmlWriteMode.IgnoreSchema);
      StreamReader sr = new StreamReader(ms, System.Text.Encoding.UTF8);
      ms.Position = 0;
      String strXml = sr.ReadToEnd();
      sr.Close();
      ms.Close();
      return strXml;


in success method json iam calling like

$.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: 'url',
                    data: "{}",
                    async: false,
                    dataType: "xml",
                    success: function (data) {
                        build_menu(data)
                    },


my returned xml data from webmethod like

<NewDataSet>
  <Table>
    <UserID>17727</UserID>
    <User>test user</User>
    <Ordering />
  </Table>
<Table1>
    <id>21</id>
    <CustomerID>12872</CustomerID>
    <Customer>Actavis Group</Customer>
    <Primarycsrid>16231</Primarycsrid>
    <PrimaryCSR>Kavitha Rani Aryamarati</PrimaryCSR>
    <ImplicitSameBranchAccess>true</ImplicitSameBranchAccess>
    <azGroup>A - C</azGroup>
    <IsLoggedOnUserCustomer>false</IsLoggedOnUserCustomer>
  </Table1>
</NewDataSet>


i want to show my out put as

{
  "NewDataSet": {
    "Table": [
      {
        "UserID": "17727",
        "User": "test user"
      },],
    "Table1": [
      {
        "id": "21",
        "CustomerID": "12872",
        "Customer": "Actavis Group",
        "Primarycsrid": "16231",
        "PrimaryCSR": "Kavitha Rani Aryamarati",
        "ImplicitSameBranchAccess": "true",
        "azGroup": "A - C",
        "IsLoggedOnUserCustomer": "false"
      },]
  }
}


how toonvert xml to json please help me

.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: 'url', data: "{}", async: false, dataType: "xml", success: function (data) { build_menu(data) },


my returned xml data from webmethod like

<NewDataSet>
  <Table>
    <UserID>17727</UserID>
    <User>test user</User>
    <Ordering />
  </Table>
<Table1>
    <id>21</id>
    <CustomerID>12872</CustomerID>
    <Customer>Actavis Group</Customer>
    <Primarycsrid>16231</Primarycsrid>
    <PrimaryCSR>Kavitha Rani Aryamarati</PrimaryCSR>
    <ImplicitSameBranchAccess>true</ImplicitSameBranchAccess>
    <azGroup>A - C</azGroup>
    <IsLoggedOnUserCustomer>false</IsLoggedOnUserCustomer>
  </Table1>
</NewDataSet>


i want to show my out put as

{
  "NewDataSet": {
    "Table": [
      {
        "UserID": "17727",
        "User": "test user"
      },],
    "Table1": [
      {
        "id": "21",
        "CustomerID": "12872",
        "Customer": "Actavis Group",
        "Primarycsrid": "16231",
        "PrimaryCSR": "Kavitha Rani Aryamarati",
        "ImplicitSameBranchAccess": "true",
        "azGroup": "A - C",
        "IsLoggedOnUserCustomer": "false"
      },]
  }
}


how toonvert xml to json please help me


Please check below link
you can use nuget package newtonsoft json[^]

and easily convert Any Collection or object to json
as generic collection , data set , data table,
make data table from xml and convert that to json .

JSON[^]


ok

Then you can use
Here dt is DataTable
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
           Dictionary<string, object> row;
           foreach (DataRow dr in dt.Rows)
           {
               row = new Dictionary<string, object>();
               foreach (DataColumn col in dt.Columns)
               {
                   row.Add(col.ColumnName, dr[col]);
               }
               rows.Add(row);
           }
           return serializer.Serialize(rows);



Use This Method

private StringBuilder DatasetToJson(DataSet ds)
       {
           var sb = new StringBuilder();
           var serializer =
               new JavaScriptSerializer();
           var rows = new List<Dictionary<string, object>>();
           Dictionary<string, object> row;
           foreach (DataTable dt in ds.Tables)
           {
               foreach (DataRow dr in dt.Rows)
               {
                   row = dt.Columns.Cast<DataColumn>().ToDictionary(col => col.ColumnName, col => dr[col]);
                   rows.Add(row);
               }
               sb.Append(serializer.Serialize(rows));
           }
           return sb;
       }