【问题标题】:Convert JSON FILE to XML FILE using POWERSHELL使用 POWERSHELL 将 JSON 文件转换为 XML 文件
【发布时间】: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


    【解决方案1】:

    您首先必须将 json 文件转换为类似的 PowerShell 对象

    $JsonObject = Get-Content -Path "C:\path\to\temp_JSON.json" | ConvertFrom-Json
    

    这可以用于参数InputObject

    $XML = ConvertTo-Xml -As Stream -InputObject $JsonObject -Depth 3
    

    注意参数Depth。您可能需要根据您的结构来玩这个,因为默认值为 1。请参阅 documentation 以供参考。

    然后可以使用命令 Out-File 将生成的 XML 对象保存到文件中

    Out-File -FilePath "C:\path\to\temp_XML.xml" -InputObject $XML 
    

    这也可以通过管道在一条线上完成。

    Get-Content -Path "C:\path\to\temp_JSON.json" | ConvertFrom-Json | ConvertTo-Xml -As Stream -Depth 3 | Out-File -FilePath "C:\path\to\temp_XML.xml"
    

    【讨论】:

    • 感谢@Guenther Schmitz,这是向前迈出的一步,我需要这样。我在 PS 控制台中尝试了行,但不幸的是它是创建的空文件 xml,里面没有数据
    • 只有这个在 xml 对象里面 --- ------- version="1.0" encoding="utf-8" Objects
    • PS C:\> 获取内容-路径 "C:\0\temp_JSON.json" | ConvertFrom-Json | ConvertTo-Xml -As Document -Depth 3 | Out-File -FilePath "C:\0\temp_XML.xml" 我用这个,我是不是哪里错了
    • json的内容是什么?您可以将其添加到您的问题中吗?
    • 我在主要问题中添加了 json 字符串,如果我从浏览器打开 json 文件显示正确
    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 2017-12-14
    • 2015-02-23
    • 2021-03-31
    • 2014-10-26
    • 1970-01-01
    • 2017-06-18
    • 2023-03-09
    相关资源
    最近更新 更多