我有如下的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.
我使用了以下在线工具
当我跳过声明xsi:type
时,它获得了成功的答案。但我需要按原样进行转换?
答案 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>
答案 1 :(得分:0)
Xml-to-json在线工具会有所帮助。我是该项目的维护者。