2017-10-04 27 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