2013-08-21 4 views
-1

JSON을 파싱하는 일부 자바 스크립트 코드가 있는데 o.products 및 cp.key에서 "options"및 "childProducts"데이터를 가져 오려고합니다. 끝없는 문자열 파싱없이 효율적으로이 작업을 수행 할 수있는 방법이 있습니까?Javascript, JSON에서 데이터 추출

var spConfig = new Product.Config(
{"attributes":{ 
    "451":{ 
     "id":"451", 
     "code":"package_size", 
     "label":"Package size", 
     "options":[ 
      {"id":"4", 
      "label":"1 lb.", 
      "oldPrice":"0", 
      "products":["4562"], 
      "brewsaver":"1"}, 
      {"id":"62", 
      "label":"1 lb. crushed", 
      "oldPrice":"0", 
      "products":["4649"], 
      "brewsaver":"1"}, 
      {"id":"14", 
      "label":"55 lbs.", 
      "oldPrice":"0", 
      "products":["704"], 
      "brewsaver":null} 
      ] 
     } 
     }, 
    "template":"$#{price}", 
    "basePrice":"1.75", 
    "oldPrice":"1.75", 
    "productId":"3390", 
    "chooseText":"Choose an Option...", 
    "taxConfig":  { 
     "includeTax":false, 
     "showIncludeTax":false, 
     "showBothPrices":false, 
     "defaultTax":0, 
     "currentTax":0, 
     "inclTaxTitle":"Incl. Tax" 
     }, 
    "childProducts": 
     { 
     "704": 
      { 
      "price":"64.99", 
      "finalPrice":"64.99" 
      }, 
     "4562": 
      { 
      "price":"1.75", 
      "finalPrice":"1.75" 
      }, 
     "4649": 
      { 
      "price":"1.75", 
      "finalPrice":"1.75" 
      } 
     }, 

답변

-1

{"attributes"로 시작하는 부분이 유효 JSON처럼 보이는, 그래서 당신이 있음을 분리하면 당신은 PHP의 중첩 세트를 얻을 수 json_decode을 사용할 수 있습니다 (처음부터 var spConfig = new Product.Config(를 제거하고 난 가정합니다 );가 끝에) 연관 배열.

그런 다음 배열을 회전시키고 일을 결합하여 문자열 파싱이 필요하지 않습니다.

+0

도움 주셔서 감사합니다. 작동 중! 그것에 6 시간을 보낸 후에 (그리고 작동하지 않는), 나는 결국 다른 중괄호가 필요하다는 것을 깨달았다. 거룩한! –

+0

@ JenniferBorn : 좋은 거래, 도움이 된 것을 기쁘게 생각합니다! –