【发布时间】:2021-01-08 03:52:18
【问题描述】:
我需要将 Json 文件从 web 转换为 xml 文件,我需要非常简单的方法......我需要来自 Json 响应的几个数据...... 周围有很多,但不是很清楚。 我发现了一些这样的示例:
PS c:\0> ConvertTo-Xml -InputObject temp_JSON.json -As String
PS c:\0> ConvertTo-Xml -InputObject temp_JSON.json -As document
但我看不到或找不到结果...我错了。
这是要转换的 Json 响应字符串:
"{"data":[{"id":"86xEyb****lUsw==","custom_fields":{"firstName":"Z***n","lastName":"Pe***ki","locale":"EN-GB_MK","timezone":"8","gender":"N\/A","phone":"+38***6","email":null},"tags":[],"created_at":1600276472}],"links":{"first":"http:\/\/app.respond.io\/api\/v1\/contact\/by_custom_field?name=firstName&value=zoran&page=1","last":"http:\/\/app.respond.io\/api\/v1\/contact\/by_custom_field?name=firstName&value=zoran&page=1","prev":null,"next":null},"meta":{"current_page":1,"from":1,"last_page":1,"path":"http:\/\/app.respond.io\/api\/v1\/contact\/by_custom_field","per_page":10,"to":1,"total":1}}"
@Guenther Schmitz 回答完美, 但我需要更多关于xml风格的问题。我收到类似的输出
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="data" Type="System.Object[]">
<Property Type="System.Management.Automation.PSCustomObject">
<Property Name="id" Type="System.String">*******</Property>
<Property Name="custom_fields" Type="System.Management.Automation.PSCustomObject">
<Property Name="firstName" Type="System.String">B***</Property>
<Property Name="lastName" Type="System.String">B***</Property> ...
但我尝试在 Json 到 xml 网络转换器进行转换,我得到了另一种 xml 格式,这对我来说更方便,我需要将数据导入数据库,我需要简单的 xml 格式,所以我需要这样的方式:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<data>
<id>nh****93UhUrQ==</id>
<custom_fields>
<firstName>BI**NA</firstName>
<lastName>B**AK</lastName>
<locale>EN_MK</locale> ...
是否可以得到另一种xml输出样式。
同时我发现如果你添加这个参数:
-notypeinformation
it will clear up types from xml, so the line would be:
$XML = ConvertTo-Xml -As Stream -InputObject $JsonObject -Depth 3 -notypeinformation
但仍然存在:
【问题讨论】:
标签: json xml powershell