2015-02-04 2 views
0

이 질문의 끝 부분에서는 ebay API 호출에서 반환 된 표준 JSON이 있습니다.JSON 구문 분석 @

나는 그 중 대부분을 구문 분석하는 데 아무런 문제가 없습니다.

var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || []; 

for (var i = 0; i < items.length; ++i) { 
    var item = items[i]; 
    var title = item.title; 
    var timeLeft= item.sellingStatus[0].timeLeft; 
} 

을하지만, 내가

var currencyID= item.sellingStatus[0].currentPrice[0][email protected]; 

시도했습니다 통화를 얻는 방법을 알아낼 수 없습니다 그러나 콘솔 화살표 나에게 잘못된 문자를 알려줍니다 타이틀을 얻기 위해, 시간이 나는 사용하고 등 왼쪽 @ 기호를 가리키며 - 그 구문을 분석하는 올바른 방법은 무엇입니까?

{ 
    "findItemsByKeywordsResponse":[ 
     { 
     "ack":[ 
      "Success" 
     ], 
     "version":[ 
      "1.12.0" 
     ], 
     "timestamp":[ 
      "2014-03-26T18:29:40.583Z" 
     ], 
     "searchResult":[ 
      { 
       "@count":"100", 
       "item":[ 
        { 
        "itemId":[ 
         "151258132867" 
        ], 
        "title":[ 
         "Apple iPhone 4 - clean esn - Black (Verizon) Smartphone" 
        ], 
        "globalId":[ 
         "EBAY-US" 
        ], 
        "primaryCategory":[ 
         { 
          "categoryId":[ 
           "9355" 
          ], 
          "categoryName":[ 
           "Cell Phones & Smartphones" 
          ] 
         } 
        ], 
        "galleryURL":[ 
         "http:\/\/thumbs4.ebaystatic.com\/m\/mGwHzpyvkph9-nU12sCspxw\/140.jpg" 
        ], 
        "viewItemURL":[ 
         "http:\/\/www.ebay.com\/itm\/Apple-iPhone-4-clean-esn-Black-Verizon-Smartphone-\/151258132867?pt=Cell_Phones" 
        ], 
        "productId":[ 
         { 
          "@type":"ReferenceID", 
          "__value__":"101787954" 
         } 
        ], 
        "paymentMethod":[ 
         "PayPal" 
        ], 
        "autoPay":[ 
         "false" 
        ], 
        "postalCode":[ 
         "93905" 
        ], 
        "location":[ 
         "Salinas,CA,USA" 
        ], 
        "country":[ 
         "US" 
        ], 
        "shippingInfo":[ 
         { 
          "shippingServiceCost":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"0.0" 
           } 
          ], 
          "shippingType":[ 
           "Free" 
          ], 
          "shipToLocations":[ 
           "US" 
          ], 
          "expeditedShipping":[ 
           "true" 
          ], 
          "oneDayShippingAvailable":[ 
           "false" 
          ], 
          "handlingTime":[ 
           "2" 
          ] 
         } 
        ], 
        "sellingStatus":[ 
         { 
          "currentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"96.0" 
           } 
          ], 
          "convertedCurrentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"96.0" 
           } 
          ], 
          "bidCount":[ 
           "17" 
          ], 
          "sellingState":[ 
           "Active" 
          ], 
          "timeLeft":[ 
           "P0DT0H0M11S" 
          ] 
         } 
        ], 
        "listingInfo":[ 
         { 
          "bestOfferEnabled":[ 
           "false" 
          ], 
          "buyItNowAvailable":[ 
           "false" 
          ], 
          "startTime":[ 
           "2014-03-19T18:29:51.000Z" 
          ], 
          "endTime":[ 
           "2014-03-26T18:29:51.000Z" 
          ], 
          "listingType":[ 
           "Auction" 
          ], 
          "gift":[ 
           "false" 
          ] 
         } 
        ], 
        "returnsAccepted":[ 
         "false" 
        ], 
        "condition":[ 
         { 
          "conditionId":[ 
           "3000" 
          ], 
          "conditionDisplayName":[ 
           "Used" 
          ] 
         } 
        ], 
        "isMultiVariationListing":[ 
         "false" 
        ], 
        "topRatedListing":[ 
         "false" 
        ] 
        }, 
        { 
        "itemId":[ 
         "360810145746" 
        ], 
        "title":[ 
         "U Apple iPhone 3GS - 8GB - Black (FACTORY UNLOCKED) Smartphone (C)" 
        ], 
        "globalId":[ 
         "EBAY-US" 
        ], 
        "subtitle":[ 
         "****USA SELLER*****FAST SHIPPING*****TOP SELLER****" 
        ], 
        "primaryCategory":[ 
         { 
          "categoryId":[ 
           "9355" 
          ], 
          "categoryName":[ 
           "Cell Phones & Smartphones" 
          ] 
         } 
        ], 
        "galleryURL":[ 
         "http:\/\/thumbs3.ebaystatic.com\/m\/mQGXy5hucEtqk6inUKFu67g\/140.jpg" 
        ], 
        "viewItemURL":[ 
         "http:\/\/www.ebay.com\/itm\/U-Apple-iPhone-3GS-8GB-Black-FACTORY-UNLOCKED-Smartphone-C-\/360810145746?pt=Cell_Phones" 
        ], 
        "productId":[ 
         { 
          "@type":"ReferenceID", 
          "__value__":"99984545" 
         } 
        ], 
        "paymentMethod":[ 
         "PayPal" 
        ], 
        "autoPay":[ 
         "true" 
        ], 
        "postalCode":[ 
         "10001" 
        ], 
        "location":[ 
         "New York,NY,USA" 
        ], 
        "country":[ 
         "US" 
        ], 
        "shippingInfo":[ 
         { 
          "shippingServiceCost":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"0.0" 
           } 
          ], 
          "shippingType":[ 
           "Free" 
          ], 
          "shipToLocations":[ 
           "US", 
           "CA", 
           "GB", 
           "AU", 
           "FR", 
           "HK", 
           "MT", 
           "IE", 
           "IL", 
           "NZ", 
           "PH", 
           "SG" 
          ], 
          "expeditedShipping":[ 
           "true" 
          ], 
          "oneDayShippingAvailable":[ 
           "false" 
          ], 
          "handlingTime":[ 
           "1" 
          ] 
         } 
        ], 
        "sellingStatus":[ 
         { 
          "currentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"84.95" 
           } 
          ], 
          "convertedCurrentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"84.95" 
           } 
          ], 
          "sellingState":[ 
           "Active" 
          ], 
          "timeLeft":[ 
           "P12DT0H9M11S" 
          ] 
         } 
        ], 
        "listingInfo":[ 
         { 
          "bestOfferEnabled":[ 
           "false" 
          ], 
          "buyItNowAvailable":[ 
           "false" 
          ], 
          "startTime":[ 
           "2013-12-08T18:33:51.000Z" 
          ], 
          "endTime":[ 
           "2014-04-07T18:38:51.000Z" 
          ], 
          "listingType":[ 
           "StoreInventory" 
          ], 
          "gift":[ 
           "false" 
          ] 
         } 
        ], 
        "returnsAccepted":[ 
         "true" 
        ], 
        "condition":[ 
         { 
          "conditionId":[ 
           "3000" 
          ], 
          "conditionDisplayName":[ 
           "Used" 
          ] 
         } 
        ], 
        "isMultiVariationListing":[ 
         "false" 
        ], 
        "discountPriceInfo":[ 
         { 
          "originalRetailPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"449.0" 
           } 
          ], 
          "pricingTreatment":[ 
           "STP" 
          ], 
          "soldOnEbay":[ 
           "false" 
          ], 
          "soldOffEbay":[ 
           "false" 
          ] 
         } 
        ], 
        "topRatedListing":[ 
         "true" 
        ] 
        } 
+2

당신이 사용할 수있는'VAR currencyID = item.sellingStatus [0] .currentPrice [0] [ '@ currencyId'] 나는 ... 그들이 포맷 할 이유가있을 때' – Biffen

답변

4

당신은 대괄호 표기법

var currencyID= item.sellingStatus[0].currentPrice[0]["@currencyId"]; 
당신은 속성이 알파로 시작하지 않는이 언제 할 필요가

, _, 또는 $ 문자로 문자열을 사용하여 속성에 액세스 할 필요가

Dot notation Property Accessor

get = object.property;

object.property = set;

속성은 올바른 자바 스크립트 식별자, 즉 숫자로 시작할 수없는 밑줄 ("_")과 달러 기호 ("$")를 포함하는 일련의 영숫자 문자 여야합니다. 예를 들어, object. $ 1은 유효하지만 object.1은 유효하지 않습니다.

+0

많은 감사, 받아 일반적인 문자열 즉 currencyId와 반대되는이 방법? –

+0

아니, 나는 그들이 그렇게하는 이유를 생각할 수 없다. 그들이 사용하는 내부 구조 일 수도있다. 아마 그들의 api의 devs는 알고있는 유일한 것입니다. –