使用Powershell将XML转换为特定的JSON结构 [英] Convert XML to specific JSON structure with powershell

查看:116
本文介绍了使用Powershell将XML转换为特定的JSON结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要有关将xml转换为特定json结构的帮助. XML看起来像这样,

Need a help on converting an xml to a specific json structure. XML would look like this,

<DataGrid> 
<DataRow>
    <RowID>1</RowID>
    <Date>26/10/2014</Date>
    <Owner>ABC Company</Owner>        
    <Make>Mitsubishi</Make>
    <Model_Family>Lancer</Model_Family>
    <Submodel>Lancer 2.0 GSR Hatch CVT</Submodel>
    <Origin/>
    <CC_Rating>1997</CC_Rating>
    <Weight>2000</Weight> 
</DataRow> 
<DataRow>
    <RowID>2</RowID>
    <Date>26/10/2014</Date>
    <Owner>ABC Company</Owner>        
    <Make>Mazda</Make>
    <Model_Family>Axela</Model_Family>
    <Submodel/>
    <Origin>Japan</Origin>
    <CC_Rating>1497</CC_Rating>
    <Weight/> 
</DataRow>
 <DataRow>
    <RowID>3</RowID>
    <Date>26/10/2014</Date>
    <Owner>Test Company</Owner>        
    <Make>Kia</Make>
    <Model_Family>Sorento</Model_Family>
    <Submodel/>
    <Origin>Korea</Origin>
    <CC_Rating>2200</CC_Rating>
    <Weight>2500<Weight> 
</DataRow>
<DataRow>
    <RowID>4</RowID>
    <Date>26/10/2014</Date>
    <Owner>Test Company</Owner>        
    <Make>Nissan</Make>
    <Model_Family>Pathfinder</Model_Family>
    <Submodel>SUV<Submodel>
    <Origin>Japan</Origin>
    <CC_Rating>2000</CC_Rating>
    <Weight>2000<Weight> 
</DataRow>

可以有上述格式的一个或多个文件,我的要求是读取所有这些文件并按所有者进行分组,然后通过将这些对象转换为JSON来调用REST Web服务.所需的JSON格式如下.

There can be one or more files in above format, my requirement is to read all those files and group them by Owner and call a REST web service by converting those object to JSON. Required JSON format will be as follows.

{
"batches": [
    {
        "Owner": "ABC Company",
        "Date": "2014-10-26",
        "Vehicles": [
            {                    
                "Make": "Mitsubishi",
                "ModelFamily": "Lancer",
                "SubModel": "Lancer 2.0 GSR Hatch CVT",
                "Origin": null
                "CcRating": "1997",
                "Weight": "2000"                    
            },
            {                   
                "Make": "Mazda",
                "ModelFamily": "Axela",
                "SubModel": null,
                "Origin": "Japan",
                "CcRating": "1497",
                "Weight": null                   
            }
        ]
    },
    {
        "Owner": "Test Company",
        "Date": "2014-10-26",
        "Vehicles": [
            {                   
                "Make": "Kia",
                "ModelFamily": "Sorento",
                "SubModel": null,
                "Origin": "Korea",
                "CcRating": "2200",
                "Weight": "2500"                  
            },
            {                    
                "Make": "Nissan",
                "ModelFamily": "Pathfinder",
                "SubModel": "SUV",
                "Origin": "Japan",
                "CcRating": "2000",
                "Weight": "2000"                   
            }
        ]
    }
]

}

这需要使用Windows Powershell来完成,我是一个Java家伙,除了调用远程ftp服务器并读取所有xml文件外,不知道如何使用Powershell来实现. 真的很感激,如果有人可以帮我这个忙.

This need to be done using windows powershell, Iam a java guy and have no idea how to do it using powershell except calling remote ftp server and read all xml files. Really appreciate, if someone could help me this.

推荐答案

Powershell有一些用于XML的东西.首先,它具有本机[XML]数据类型.您可以像这样将字符串转换为XML:

Powershell has some stuff for working with XML. First, it has a native [XML] datatype. You can cast strings to XML like so:

$someXml = '...' # pretend there's XML in there!
$xmlObject = [XML]$someXml

ConvertTo-Xml cmdlet接受字符串或其他对象,并将其转换为XML(作为文档(XML对象),字符串或流(字符串数组)).您可以将字符串或对象传递给它:

The ConvertTo-Xml cmdlet takes a string or other object and converts it to XML, either as a Document (XML object), a string, or a stream (array of strings). You can pass to it a string, or an object:

$xmlObject = [XML](Get-Content -Path C:\Path\to\my.xml)
$xmlObject = Get-Content -Path C:\Path\to\my.xml | ConvertTo-Xml

使用XML对象

有了对象后,就可以将节点作为属性来访问:

Working with XML Objects

Once you've got your object, you can access nodes as properties:

$xmlObject.SomeNode.SomeChild

您还可以使用XPATH选择单个节点或多个节点:

You can also use XPATH to select a single node or multiple nodes:

$xmlObject.SelectSingleNode("//this[1]")
$xmlObject.SelectNodes("//these")

或者以更强大的方式实现它,您可以使用Select-Xml cmdlet:

Or to do it in a more powershell way you might use the Select-Xml cmdlet:

$xmlObject | Select-Xml "//these"

我遗漏了许多其他内容,尤其是操作方面的内容,因为似乎您只需要查找信息并将其分组在一起即可.

I'm leaving out a lot of other stuff, especially manipulation, because it seems like you just need to find information and group it together.

在powershell中对JSON的了解不多.

There isn't a lot to know about JSON in powershell.

使用ConvertFrom-JSON将现有JSON转换为对象,然后使用ConvertTo-JSON将对象转换为JSON字符串.

Use ConvertFrom-JSON to convert existing JSON into an object, and use ConvertTo-JSON to convert an object into a JSON string.

有时候,我们称哈希为哈希或关联数组,我确信您已经知道了.在Powershell中,您可以像这样使用它们:

Sometimes called hashes or associate arrays, as I'm sure you're aware. In Powershell you use them like this:

$hash = @{
    Key1 = 'Value1'
    Key2 = 'Value2'
    Key3 = 10
}

# on one line:
$hash = @{ Key1='Val1';Key2='Val2' }

# adding pairs
$hash['NewKey'] = 'NewVal'
$hash.NewKey = 'NewVal'

# retrieving
$hash['NewKey']
$hash.NewKey

该值可以是数组,对象,另一个哈希等.

The value can be an array, an object, another hash, etc.

$complex = @{
    ThisThing = @{
        Key1 = 'val1'
        Key2 = 5
    } 
}

REST

Invoke-RestMethod是在Powershell中进行REST调用的最简单方法(需要3.0+版本).

REST

Invoke-RestMethod is the easiest way to make REST calls in Powershell (requires version 3.0+).

一旦您能够从XML中解析出信息,就构建包含所需结构的嵌套哈希或哈希数组,然后将其转换为JSON:

Once you're able to parse the information out of the XML, build a nested hash or array of hashes that contains the structure you want, and then convert it to JSON:

$mySpecialHash | ConvertTo-JSON

特别注意数组和哈希在结果JSON中的表示方式,并可能更改构建它们的方式以获取所需的输出.

Take special note of how arrays and hashes are represented in the resulting JSON and maybe change the way you're building them to get the output you want.

如果您对特定方法或一段代码有特定疑问,请在SO上针对该特定部分或代码发表特定疑问.

If you have specific questions about a particular method or piece of code, then post specific questions on SO about that piece of it.

  • A very detailed and useful page on working with XML in Powershell
  • ConvertTo-Xml
  • Select-Xml

这篇关于使用Powershell将XML转换为特定的JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆