用js方式取得接口里面json數據的key和value值


大家在實際操作中難免遇到對接口的問題,想必對一些小白來說取得里面想要是數據也是很是頭疼,那么接下來我會結合接口實際情況教大家怎么取得里面相應的數據

接口數據例如:(數據為 模擬數據,json格式)

{
    "month": {
        "monthly": ["2018年4月", "2018年5月"]
    },
    "nameAndRateOperation": {
        "濟南華陽炭素有限公司": ["91.67", "91.67"],
        "濟南萬瑞炭素有限責任公司": ["84", "84"],
        "濟南龍山炭素有限公司": ["85.71", "85.71"],
        "德州歐萊恩永興碳素有限公司": ["70", "80"],
        "德州永興碳素有限公司": ["90", "90"],
        "德州乾海碳素有限公司": ["70", "75"],
        "濟南海川投資集團有限公司": ["81.25", "81.25"],
        "濟南匯豐炭素有限公司": ["86", "86"],
        "山東金利達碳素有限公司": ["86.68", "80"],
        "濟南澳海炭素有限公司": ["85", "85"]
    },
    "nameAndYield": {
        "濟南華陽炭素有限公司": ["1.2", "1.2"],
        "濟南萬瑞炭素有限責任公司": ["4", "4"],
        "濟南龍山炭素有限公司": ["2.4", "2.4"],
        "德州歐萊恩永興碳素有限公司": ["0.8", "1"],
        "德州永興碳素有限公司": ["0.67", "0.5"],
        "德州乾海碳素有限公司": ["0.2", "0.25"],
        "濟南海川投資集團有限公司": ["1.6", "1.6"],
        "濟南匯豐炭素有限公司": ["0.8", "0.8"],
        "山東金利達碳素有限公司": ["2", "2.5"],
        "濟南澳海炭素有限公司": ["4.33", "4.5"]
    },
    "nameAndDateTime": {
        "濟南華陽炭素有限公司": ["2018年4月", "2018年5月"],
        "濟南萬瑞炭素有限責任公司": ["2018年4月", "2018年5月"],
        "濟南龍山炭素有限公司": ["2018年4月", "2018年5月"],
        "德州歐萊恩永興碳素有限公司": ["2018年4月", "2018年5月"],
        "德州永興碳素有限公司": ["2018年4月", "2018年5月"],
        "德州乾海碳素有限公司": ["2018年4月", "2018年5月"],
        "濟南海川投資集團有限公司": ["2018年4月", "2018年5月"],
        "濟南匯豐炭素有限公司": ["2018年4月", "2018年5月"],
        "山東金利達碳素有限公司": ["2018年4月", "2018年5月"],
        "濟南澳海炭素有限公司": ["2018年4月", "2018年5月"]
    },
    "regionAndCompany": {
        "華東": ["德州歐萊恩永興碳素有限公司", "德州乾海碳素有限公司", "德州永興碳素有限公司", "山東金利達碳素有限公司", "濟南澳海炭素有限公司", "濟南海川投資集團有限公司", "濟南華陽炭素有限公司", "濟南匯豐炭素有限公司", "濟南龍山炭素有限公司", "濟南萬瑞炭素有限責任公司"]
    },
    "nameAndCapacity": {
        "濟南華陽炭素有限公司": ["6", "6"],
        "濟南萬瑞炭素有限責任公司": ["5", "5"],
        "濟南龍山炭素有限公司": ["7", "7"],
        "德州歐萊恩永興碳素有限公司": ["30", "30"],
        "德州永興碳素有限公司": ["20", "20"],
        "德州乾海碳素有限公司": ["20", "20"],
        "濟南海川投資集團有限公司": ["8", "8"],
        "濟南匯豐炭素有限公司": ["10", "10"],
        "山東金利達碳素有限公司": ["15", "15"],
        "濟南澳海炭素有限公司": ["10", "10"]
    },
    "storageWithCompany": {
        "濟南華陽炭素有限公司": ["0.8", "0.8"],
        "濟南萬瑞炭素有限責任公司": ["3.2", "3.2"],
        "濟南龍山炭素有限公司": ["2.2", "2.2"],
        "德州歐萊恩永興碳素有限公司": ["1.2", "1.2"],
        "德州永興碳素有限公司": ["0.5", "0.5"],
        "德州乾海碳素有限公司": ["0.15", "0.15"],
        "濟南海川投資集團有限公司": ["1", "1"],
        "濟南匯豐炭素有限公司": ["0.5", "1"],
        "山東金利達碳素有限公司": ["1.5", "1.5"],
        "濟南澳海炭素有限公司": ["2.5", "2.5"]
    }
}
json數據
               //取出來所有的地區:華南華北....
                        for (var key in data.regionAndCompany) {
                            region.push(key);
                        }
                        // console.log(region)
                        
                        //接口取出來的倉儲的數組
                        for (key in data.storageWithCompany) {
                            storageWithCompany.push(data.storageWithCompany[key])
                        }
                        // console.log(storageWithCompany)

                        // //接口取出來的產能的數組
                        // for(key in data.nameAndCapacity){
                        //     capacity.push(data.nameAndCapacity[key])
                        // }
                        // // console.log(capacity)

                        //接口取出來的開工率的數組
                        for (key in data.nameAndRateOperation) {
                            rateOperation.push(data.nameAndRateOperation[key])
                        }
                        // console.log(rateOperation)

                        //全部的公司名字
                        for (key in data.nameAndRateOperation) {
                            companyRateOperation.push(key);
                        }
                        // console.log(companyRateOperation)
                        
                        //全部的時間數據,二維數組
                        for (var key in data.nameAndDateTime) {
                            date.push(data.nameAndDateTime[key])
                        }
                        // console.log(date)
                        
                        //全部的產量數組,二維,每個公司的產量是一個數組
                        for (var key in data.nameAndYield) {
                            yield.push(data.nameAndYield[key])
                        }
                        // console.log(yield)

 

再放上網上的方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM