使用在线工具无法转换XML到JSON

问题描述 投票:0回答:2

我有一个XML数据如下图所示,

                      <Request>
                        <SourceCredentials>
                           <SourceName>{SourceName}</SourceName>
                           <Password>{Password}</Password>
                           <SiteIDs>
                              <int>{SiteID}</int>
                           </SiteIDs>
                        </SourceCredentials>
                        <UserCredentials>
                           <Username>{Username}</Username>
                           <Password>{Password}</Password>
                           <SiteIDs>
                              <int>{SiteID}</int>
                           </SiteIDs>
                        </UserCredentials>
                        <XMLDetail>Full</XMLDetail>
                        <PageSize>10</PageSize>
                        <CurrentPageIndex>0</CurrentPageIndex>
                        <ClientID>snarf</ClientID>
                        <Test>true</Test>
                        <CartItems>
                           <CartItem>
                              <DiscountAmount>4</DiscountAmount>
                              <Quantity>1</Quantity>
                              <Item xsi:type="Service">
                                 <ID>000123</ID>
                              </Item>
                           </CartItem>
                        </CartItems>
                        <Payments>
                           <PaymentInfo xsi:type="CreditCardInfo">
                              <CreditCardNumber>{CreditCardNumber}</CreditCardNumber>
                              <Amount>5</Amount>
                              <BillingAddress>123 Happy Ln</BillingAddress>
                              <BillingCity>San Luis Obispo</BillingCity>
                              <BillingState>CA</BillingState>
                              <BillingPostalCode>93405</BillingPostalCode>
                              <ExpYear>2014</ExpYear>
                              <ExpMonth>7</ExpMonth>
                              <BillingName>Bob Joe</BillingName>
                           </PaymentInfo>
                        </Payments>
                     </Request>

我需要把这些转化成JSON格式。

当我使用的在线工具我有一个错误,如下面,

Unable to format the JSON output. The prefix "xsi" for attribute "xsi:type" associated with an element type "Item" is not bound.

我用下面的在线工具,

freeformatter

utilities-online.info

它让当我跳过声明xsi:type成功的答案。但我需要转换器吗?

xml json
2个回答
2
投票

这不是有效的XML因此不会成功转换。你需要在你的XML某处定义xsi前缀,使之有效的命名空间前缀。

例如,具有前缀在XML根元素声明,您的XML使用Freeformatter工具成功地转换为JSON:

<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    ....
    <CartItems>
       <CartItem>
          <DiscountAmount>4</DiscountAmount>
          <Quantity>1</Quantity>
          <Item xsi:type="Service">
             <ID>000123</ID>
          </Item>
       </CartItem>
    </CartItems>
    ....
</Request>

相关讨论:is the xsi: prefix assumed to be known in XML?


0
投票

托管在GitHub上Xml-to-json在线工具将帮助。我是这个项目的维护者。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.