json和jquery提取@string

时间:2012-06-10 12:29:47

标签: jquery json

  

可能重复:
  Accessing Json fields with weird characters
  Parsing JSON w/ @ symbol in it

大家好,这是我第一次来这里,对不起我的英语 所以我从json中提取字符串时出现问题,结果字符串看起来像@string。这是结果格式的示例

"RateInfo": {
            "@priceBreakdown": "true",
            "@promo": "false",
            "@rateChange": "false",
            "ChargeableRateInfo": {
                "@averageBaseRate": "93.33",
                "@averageRate": "93.33",
                "@commissionableUsdTotal": "93.33",
                "@currencyCode": "USD",
                "@maxNightlyRate": "93.33",
                "@nightlyRateTotal": "93.33",
                "@surchargeTotal": "16.33",
                "@total": "109.66",
                "NightlyRatesPerRoom": {

我需要出一个@total。任何帮助,请

2 个答案:

答案 0 :(得分:0)

假设您已经将JSON字符串解析为JavaScript对象,例如:

var obj = JSON.parse(jsonString);

然后您可以像平常一样简单地访问@total属性:

var total = obj.RateInfo.ChargeableRateInfo["@total"];

请注意,您必须使用方括号来访问@total属性,因为@字符在JavaScript标识符中无效。

答案 1 :(得分:-1)

为什么不将XML作为XML发送,而不是将其转换为JSON?

<RateInfo priceBreakdown="true" promo="false" rateChange="false">
     <ChargeableRateInfo averageBaseRate="93.33" averageRate="93.33"
              commissionableUsdTotal="93.33" currencyCode="USD"
              maxNightlyRate="93.33"  nightlyRateTotal="93.33"
              surchargeTotal="16.33" total="109.66">
         <NightlyRatesPerRoom>
               ...
         </NightlyRatesPerRoom>
    </ChargeableRateInfo>
</RateInfo>