【发布时间】:2019-10-16 11:32:46
【问题描述】:
我想将 XML 列转换为 JSON 字符串。
XML:
<Policies>
<Policy>
<GroupUserName>testX</GroupUserName>
<Roles>
<Role>
<Name>Browser</Name>
<Description>May view folders, reports and subscribe to reports.</Description>
</Role>
<Role>
<Name>Content Manager</Name>
<Description>May manage content in the Report Server. This includes folders, reports and resources.</Description>
</Role>
</Roles>
</Policy>
<Policy>
<GroupUserName>meme</GroupUserName>
<Roles>
<Role>
<Name>Browser</Name>
<Description>May view folders, reports and subscribe to reports.</Description>
</Role>
<Role>
<Name>My Reports</Name>
<Description>May publish reports and linked reports; manage folders, reports and resources in a users My Reports folder.</Description>
</Role>
</Roles>
</Policy>
<Policy>
<GroupUserName>test980706</GroupUserName>
<Roles>
<Role>
<Name>Browser</Name>
<Description>May view folders, reports and subscribe to reports.</Description>
</Role>
</Roles>
</Policy>
<Policy>
<GroupUserName>Admin</GroupUserName>
<Roles>
<Role>
<Name>Browser</Name>
<Description>May view folders, reports and subscribe to reports.</Description>
</Role>
<Role>
<Name>Content Manager</Name>
<Description>May manage content in the Report Server. This includes folders, reports and resources.</Description>
</Role>
<Role>
<Name>My Reports</Name>
<Description>May publish reports and linked reports; manage folders, reports and resources in a users My Reports folder.</Description>
</Role>
<Role>
<Name>Publisher</Name>
<Description>May publish reports and linked reports to the Report Server.</Description>
</Role>
<Role>
<Name>Report Builder</Name>
<Description>May view report definitions.</Description>
</Role>
</Roles>
</Policy>
</Policies>
JSON:
{
"Id": "00000000-0000-0000-0000-000000000000",
"InheritParentPolicy": false"Policies": [
{
"GroupUserName": "testX",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "Admin",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
},
{
"Name": "Content Manager",
"Description": "May manage content in the Report Server. This includes folders, reports and resources."
},
{
"Name": "My Reports",
"Description": "May publish reports and linked reports; manage folders, reports and resources in a users My Reports folder."
},
{
"Name": "Publisher",
"Description": "May publish reports and linked reports to the Report Server."
},
{
"Name": "Report Builder",
"Description": "May view report definitions."
}
]
},
{
"GroupUserName": "meme",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
},
{
"Name": "My Reports",
"Description": "May publish reports and linked reports; manage folders, reports and resources in a users My Reports folder."
}
]
},
{
"GroupUserName": "test980706",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "ETL",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "Artin",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "mamad",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "Salour",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "Zargar",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "AUser",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
},
{
"GroupUserName": "TUser",
"Roles": [
{
"Name": "Browser",
"Description": "May view folders, reports and subscribe to reports."
}
]
}
]
}
【问题讨论】:
-
太棒了!感谢您让我们知道,祝您好运。如果您遇到困难,请提出问题,向我们展示您的尝试并解释为什么它不起作用。
-
我的建议:不要。问题是 T-SQL 需要固定的、预定义的结果集。这意味着您必须使用可用的各种方法(
.nodes()、.query()、.value())将 XML 分解为表,然后然后您可以将这些表重新组合成JSON(简单的部分,FOR JSON)。这非常笨拙,纯字符串替换也好不了多少(由于 T-SQL 在这方面的功能有限)。如果要将 XML 转换为 JSON,最好的地方是“除 SQL Server 本身之外的任何地方”。让客户端代码处理这个问题。
标签: sql-server sql-server-2017