无法使用在线工具将XML转换为Json

时间:2014-08-19 11:49:50

标签: xml json

我有如下的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时,它获得了成功的答案。但我需要按原样进行转换?

2 个答案:

答案 0 :(得分:2)

它不是有效的XML因此无法成功转换。您需要在XML中的某处定义xsi前缀,以使其成为有效的名称空间前缀。

例如,如果在XML根元素处声明了前缀,则使用Freeformatter工具将XML成功转换为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?

答案 1 :(得分:0)

托管在github上的

Xml-to-json在线工具会有所帮助。我是该项目的维护者。

enter image description here