简体   繁体   中英

Parse JSON array and object in JavaScript?

I'm using Google analytics API to get a JSON message from the server. The message I receive is this one :

{
  "reports": [
    {
      "columnHeader": {
        "dimensions": [
          "ga:landingPagePath"
        ],
        "metricHeader": {
          "metricHeaderEntries": [
            {
              "name": "ga:pageviews",
              "type": "INTEGER"
            },
            {
              "name": "ga:sessions",
              "type": "INTEGER"
            }
          ]
        }
      },
      "data": {
        "rows": [
          {
            "dimensions": [
              "/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html"
            ],
            "metrics": [
              {
                "values": [
                  "1",
                  "1"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/267249-1.compliance-alex.xyz"
            ],
            "metrics": [
              {
                "values": [
                  "29",
                  "10"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/267249-1.compliance-don.xyz"
            ],
            "metrics": [
              {
                "values": [
                  "27",
                  "9"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/267249-1.compliance-fred.xyz"
            ],
            "metrics": [
              {
                "values": [
                  "20",
                  "7"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/abohar/axis-bank-the-fazilka-central-cooperative-bank-ltd-branch_abohar_frp_135.html"
            ],
            "metrics": [
              {
                "values": [
                  "1",
                  "1"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/about-us/career.htm"
            ],
            "metrics": [
              {
                "values": [
                  "8",
                  "5"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/about-us/company-profile.htm"
            ],
            "metrics": [
              {
                "values": [
                  "34",
                  "14"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/about-us/infrastructure.htm"
            ],
            "metrics": [
              {
                "values": [
                  "3",
                  "1"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/adilabad/gk-hospital-multispeciality-care_adilabad_adi_399806.html"
            ],
            "metrics": [
              {
                "values": [
                  "2",
                  "1"
                ]
              }
            ]
          },
          {
            "dimensions": [
              "/ahmedabad/akhani-jagdish-kumar_ahmedabad_ahd_1124498.html"
            ],
            "metrics": [
              {
                "values": [
                  "7",
                  "3"
                ]
              }
            ]
          }
        ],
        "totals": [
          {
            "values": [
              "3420452",
              "1333496"
            ]
          }
        ],
        "rowCount": 347614,
        "minimums": [
          {
            "values": [
              "0",
              "1"
            ]
          }
        ],
        "maximums": [
          {
            "values": [
              "56660",
              "49274"
            ]
          }
        ],
        "isDataGolden": true
      },
      "nextPageToken": "1000"
    }
  ]
}  

I want to parse it and saved data in variable. How will I parse it. I tried many options but didn't get any data from JSON. Result is showing like undefined . I want to fetch the array data of dimensions and values like:

var a = "/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html";

var b = 1;

var c = 1;

Supposing your JSON input is stored in the json variable, you could just do:

 var json = '{"reports":[{"columnHeader":{"dimensions":["ga:landingPagePath"],"metricHeader":{"metricHeaderEntries":[{"name":"ga:pageviews","type":"INTEGER"},{"name":"ga:sessions","type":"INTEGER"}]}},"data":{"rows":[{"dimensions":["/-chandigarh/axis-bank-sarsini-branch_chandigarh_chg_850458.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/267249-1.compliance-alex.xyz"],"metrics":[{"values":["29","10"]}]},{"dimensions":["/267249-1.compliance-don.xyz"],"metrics":[{"values":["27","9"]}]},{"dimensions":["/267249-1.compliance-fred.xyz"],"metrics":[{"values":["20","7"]}]},{"dimensions":["/abohar/axis-bank-the-fazilka-central-cooperative-bank-ltd-branch_abohar_frp_135.html"],"metrics":[{"values":["1","1"]}]},{"dimensions":["/about-us/career.htm"],"metrics":[{"values":["8","5"]}]},{"dimensions":["/about-us/company-profile.htm"],"metrics":[{"values":["34","14"]}]},{"dimensions":["/about-us/infrastructure.htm"],"metrics":[{"values":["3","1"]}]},{"dimensions":["/adilabad/gk-hospital-multispeciality-care_adilabad_adi_399806.html"],"metrics":[{"values":["2","1"]}]},{"dimensions":["/ahmedabad/akhani-jagdish-kumar_ahmedabad_ahd_1124498.html"],"metrics":[{"values":["7","3"]}]}],"totals":[{"values":["3420452","1333496"]}],"rowCount":347614,"minimums":[{"values":["0","1"]}],"maximums":[{"values":["56660","49274"]}],"isDataGolden":true},"nextPageToken":"1000"}]}' // Parse the JSON into the data variable var data = JSON.parse(json); data.reports.forEach(report => { report.data.rows.forEach(row => { // row.dimensions will contain your 'dimensions' array console.log(row.dimensions); row.metrics.forEach(metric => { // metric.values will contain your 'values' array console.log(metric.values); }); }); }); 

You will just have to store these properties into your own variables.

Use json.parse

  var json = '{"param1":1,"param2":2}', obj = JSON.parse(json); alert(obj.param1); 

Most browsers eg Google support JSON.parse, moreover for those which don't support you can use json2 -> https://github.com/douglascrockford/JSON-js/blob/master/json2.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM