0

Possible Duplicate:
Accessing Json fields with weird characters
Parsing JSON w/ @ symbol in it

hi to everybody this is my first time here and sorry for my English so I have a problem extracting a string from json result the string look like this @string. here is a example for a result format

"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": {

i need to get out a @total. any help please

Community
  • 1
  • 1
outman
  • 147
  • 1
  • 8

2 Answers2

0

Assuming you have already parsed your JSON string into a JavaScript object e.g.:

var obj = JSON.parse(jsonString);

Then you can simply access the @total property like normal:

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

Notice that you have to use square brackets to access the @total property, since the @ character is invalid in JavaScript identifiers.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
-1

Why not send XML as XML, rather than converting it to 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> 
Eric
  • 95,302
  • 53
  • 242
  • 374
  • tanks user566527 but i try to do it using json at faster than xml i already know how using xml – outman Jun 10 '12 at 12:55
  • @user1447313: Are you sure json is faster than XML? a json-encoded xml packet is about the same size as the original XML. – Eric Jun 10 '12 at 13:06
  • yeah it true but using alot arrays and functions foreach... json comes faster... maybe me i dont usi it good – outman Jun 10 '12 at 13:17