2017-10-04 29 views
-4

我不是很喜歡JavaScriptJSON我對應用程序有以下疑問。這個JavaScript JSON「操縱」到底是什麼?

我有這個JSON文件:

{ 
    "Markets": { 
     "Market": { 
      "market_name": "Kibuye", 
      "market_description": "Kibuye Village Market", 
      "localization_id": 1, 
      "long": 29.371491, 
      "lat": -2.17897, 
      "country": "Rwanda", 
      "regione": { 
       "@nil": "true" 
      }, 
      "province": { 
       "@nil": "true" 
      }, 
      "city": { 
       "@nil": "true" 
      }, 
      "district": { 
       "@nil": "true" 
      }, 
      "town": { 
       "@nil": "true" 
      }, 
      "village": { 
       "@nil": "true" 
      }, 
      "commodity": { 
       "el": [{ 
         "market_name": "Kibuye", 
         "commodity_name": "Rice-Asia", 
         "image_link": "Rice-Asia.jpg", 
         "commodity_details_id": 1 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Apple banana", 
         "image_link": "Apple_banana.jpg", 
         "commodity_details_id": 2 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Avocado", 
         "image_link": "Avocado.jpg", 
         "commodity_details_id": 3 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Red onion", 
         "image_link": "Red_onion.jpg", 
         "commodity_details_id": 4 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "White onion", 
         "image_link": "White_onion.jpg", 
         "commodity_details_id": 5 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Green Beans", 
         "image_link": "Green_Beans.jpg", 
         "commodity_details_id": 6 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Bell pepper", 
         "image_link": "Bell_pepper.jpg", 
         "commodity_details_id": 7 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Carrot", 
         "image_link": "Carrot.jpg", 
         "commodity_details_id": 8 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Tomatoes", 
         "image_link": "Tomatoes.jpg", 
         "commodity_details_id": 9 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Irish potatoes", 
         "image_link": "Irish_potatoes.jpg", 
         "commodity_details_id": 10 
        }, { 
         "market_name": "Kibuye", 
         "commodity_name": "Maize", 
         "image_link": "Maize.jpg", 
         "commodity_details_id": 11 
        } 
       ] 
      } 
     } 
    } 
} 

然後進入我的JavaScript代碼,我有這樣的事情:

var payload = JSON.parse(pl_string); 

// create new response   
var response = payload.Markets.Market; 

pl_string參數包含以前的JSON文件。

第二條指令究竟幹什麼?

我認爲把這個JSON節到響應變量(市場的內容 - >市場領域:

 "market_name": "Kibuye", 
     "market_description": "Kibuye Village Market", 
     "localization_id": 1, 
     "long": 29.371491, 
     "lat": -2.17897, 
     "country": "Rwanda", 
     "regione": { 
      "@nil": "true" 
     }, 
     "province": { 
      "@nil": "true" 
     }, 
     "city": { 
      "@nil": "true" 
     }, 
     "district": { 
      "@nil": "true" 
     }, 
     "town": { 
      "@nil": "true" 
     }, 
     "village": { 
      "@nil": "true" 
     }, 
     "commodity": { 
      "el": [{ 
        "market_name": "Kibuye", 
        "commodity_name": "Rice-Asia", 
        "image_link": "Rice-Asia.jpg", 
        "commodity_details_id": 1 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Apple banana", 
        "image_link": "Apple_banana.jpg", 
        "commodity_details_id": 2 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Avocado", 
        "image_link": "Avocado.jpg", 
        "commodity_details_id": 3 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Red onion", 
        "image_link": "Red_onion.jpg", 
        "commodity_details_id": 4 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "White onion", 
        "image_link": "White_onion.jpg", 
        "commodity_details_id": 5 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Green Beans", 
        "image_link": "Green_Beans.jpg", 
        "commodity_details_id": 6 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Bell pepper", 
        "image_link": "Bell_pepper.jpg", 
        "commodity_details_id": 7 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Carrot", 
        "image_link": "Carrot.jpg", 
        "commodity_details_id": 8 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Tomatoes", 
        "image_link": "Tomatoes.jpg", 
        "commodity_details_id": 9 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Irish potatoes", 
        "image_link": "Irish_potatoes.jpg", 
        "commodity_details_id": 10 
       }, { 
        "market_name": "Kibuye", 
        "commodity_name": "Maize", 
        "image_link": "Maize.jpg", 
        "commodity_details_id": 11 
       } 
      ] 

難道還是我失去了一些東西

+0

呀,漂亮多了。 – deceze

+1

如果你在代碼中放置了一個調試器,你會發現基本上你可以「導航」JSON並根據你需要的響應的哪一部分創建變量 – Anon

+0

因此,如何調試代碼?只要運行它,試試看,看看會發生什麼。我有點困惑,爲什麼你作爲一個10k的用戶,沒有顯示出實際上只是嘗試代碼的跡象... – Cerbrus

回答

1

?我把它作爲答案,儘管我認爲是基本的

debugger; 
var payload = JSON.parse(pl_string); 

// create new response   
var response = payload.Markets.Market; 

如果你把t他可以很容易地看到代碼的作用,基本上通過JSON「瀏覽」並讓你看到它中的信息。

例如,在基礎的例子:

var marketName= payload.Markets.Market.market_name; 

你可以看到,marketName將"Kibuye"

+0

通常,你想避免'調試器'。相反,在瀏覽器中設置腳本中的斷點。 – Cerbrus

+0

我也在我的代碼中添加了斷點,我只是根據代碼粘貼了我們的代碼來回答 – Anon