JSON代码怎么转换成XML代码?
[{"id":1,"text":"系统维护","children":[{"id":"user","text":"人员维护"},{"id":"role","text":"角...
[ {
"id" : 1,
"text" : "系统维护",
"children" : [ {
"id" : "user",
"text" : "人员维护"
}, {
"id" : "role",
"text" : "角色维护",
"iconCls" : "icon-reload"
} ]
}, {
"text" : "开发维护",
"children" : [ {
"text" : "元数据",
"state" : "closed",
"children" : [ {
"text" : "编辑样式"
}, {
"id":"dataview",
"text" : "数据视图"
} ]
}, {
"text" : "权限资源"
}]
} ]
这段代码怎么转换成XML代码? 展开
"id" : 1,
"text" : "系统维护",
"children" : [ {
"id" : "user",
"text" : "人员维护"
}, {
"id" : "role",
"text" : "角色维护",
"iconCls" : "icon-reload"
} ]
}, {
"text" : "开发维护",
"children" : [ {
"text" : "元数据",
"state" : "closed",
"children" : [ {
"text" : "编辑样式"
}, {
"id":"dataview",
"text" : "数据视图"
} ]
}, {
"text" : "权限资源"
}]
} ]
这段代码怎么转换成XML代码? 展开
2个回答
展开全部
你可以把xml先转成DataTable再用这个方法转成JSON
DataSet(DataTable)与XML互转:http://blog.csdn.net/onlyzhangqin/archive/2008/04/05/2253965.aspx
public static string DataTableToJSON(DataTable table)
{
StringBuilder json = new StringBuilder();
json.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
// json object
json.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
string columnName = table.Columns[j].ColumnName;
string columnType = table.Columns[j].DataType.Name;
// json field
if (columnType == "Int32" || columnType == "Int16" || columnType == "Decimal")
{
// don't surround numbers with quotes
json.AppendFormat("\"{0}\":\"{1}\"", columnName, row.IsNull(columnName) ? "" : row[columnName]);
}
else if (columnType == "Boolean")
{
// make the bool value lowercase
json.AppendFormat("\"{0}\":{1}", columnName, row.IsNull(columnName) ? "" : row[columnName].ToString().ToLower());
}
else
{
// everything else gets quotes around the data
json.AppendFormat("\"{0}\":\"{1}\"", columnName, row[columnName]);
}
if (j < table.Columns.Count - 1) json.Append(","); // add comma if not last column
}
json.Append("}");
if (i < table.Rows.Count - 1) json.Append(","); // add comma if not last row
}
json.Append("]");
return json.ToString();
}
DataSet(DataTable)与XML互转:http://blog.csdn.net/onlyzhangqin/archive/2008/04/05/2253965.aspx
public static string DataTableToJSON(DataTable table)
{
StringBuilder json = new StringBuilder();
json.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
// json object
json.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
string columnName = table.Columns[j].ColumnName;
string columnType = table.Columns[j].DataType.Name;
// json field
if (columnType == "Int32" || columnType == "Int16" || columnType == "Decimal")
{
// don't surround numbers with quotes
json.AppendFormat("\"{0}\":\"{1}\"", columnName, row.IsNull(columnName) ? "" : row[columnName]);
}
else if (columnType == "Boolean")
{
// make the bool value lowercase
json.AppendFormat("\"{0}\":{1}", columnName, row.IsNull(columnName) ? "" : row[columnName].ToString().ToLower());
}
else
{
// everything else gets quotes around the data
json.AppendFormat("\"{0}\":\"{1}\"", columnName, row[columnName]);
}
if (j < table.Columns.Count - 1) json.Append(","); // add comma if not last column
}
json.Append("}");
if (i < table.Rows.Count - 1) json.Append(","); // add comma if not last row
}
json.Append("]");
return json.ToString();
}
追问
哪来的XML?
展开全部
网上都有XML和json互相转换的工具,在线就可以转换。注意XML必须有单一的根节点。
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<node>
<id>1</id>
<text>系统维护</text>
<children>
<id>user</id>
<text>人员维护</text>
</children>
<children>
<id>role</id>
<text>角色维护</text>
<iconCls>icon-reload</iconCls>
</children>
</node>
<node>
<text>开发维护</text>
<children>
<text>元数据</text>
<state>closed</state>
<children>
<text>编辑样式</text>
</children>
<children>
<id>dataview</id>
<text>数据视图</text>
</children>
</children>
<children>
<text>权限资源</text>
</children>
</node>
</root>
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<node>
<id>1</id>
<text>系统维护</text>
<children>
<id>user</id>
<text>人员维护</text>
</children>
<children>
<id>role</id>
<text>角色维护</text>
<iconCls>icon-reload</iconCls>
</children>
</node>
<node>
<text>开发维护</text>
<children>
<text>元数据</text>
<state>closed</state>
<children>
<text>编辑样式</text>
</children>
<children>
<id>dataview</id>
<text>数据视图</text>
</children>
</children>
<children>
<text>权限资源</text>
</children>
</node>
</root>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询