在Javascript中读取Json中的属性

时间:2015-10-01 08:40:14

标签: javascript json

我有问题。如何在javascript 中的 Json对象中获得 refcat 。请找到以下代码段。

{
  "request": {
    "operation": "Viviendas",
    "languague": "es",
    "idMunicipio": "5",
    "idVia": "196",
    "idProvincia": "28",
    "pc1": "9115501",
    "pc2": "VK6891N",
    "numero": "1"
  },
  "created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015",
  "created_server": 20151001092458,
  "data": [
    {
      "portal": "",
      "escaleras": [
        {
          "escalera": "1",
          "plantas": [
            {
              "planta": "00",
              "puertas": [
                {
                  "puerta": "01",
                  "refCat": "9115501VK6891N0001GB",
                  "superficieVivienda": 60.0,
                  "superficieTotal": 60.0
                }
              ]
            },
            {
              "planta": "01",
              "puertas": [
                {
                  "puerta": "01",
                  "refCat": "9115501VK6891N0002HZ",
                  "superficieVivienda": 154.0,
                  "superficieTotal": 170.0
                }
              ]
            },
            {
              "planta": "02",
              "puertas": [
                {
                  "puerta": "01",
                  "refCat": "9115501VK6891N0003JX",
                  "superficieVivienda": 161.0,
                  "superficieTotal": 177.0
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "page": "1",
  "pagesTotal": "1",
  "status_code": "200",
  "status_text": "OK"
}

我尝试使用以下方法解决此问题。

alert(json.data[0].escaleras[0].plantas[0].puertas[i].refCat);

但它不起作用。有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:0)

问题是

alert(json.data[0].escaleras[0].plantas[0].puertas[i].refCat);
                    i is causing the problem...... ^  

它应该是0

alert(json.data[0].escaleras[0].plantas[0].puertas[0].refCat);  



var json =     {
      "request": {
        "operation": "Viviendas",
        "languague": "es",
        "idMunicipio": "5",
        "idVia": "196",
        "idProvincia": "28",
        "pc1": "9115501",
        "pc2": "VK6891N",
        "numero": "1"
      },
      "created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015",
      "created_server": 20151001092458,
      "data": [
        {
          "portal": "",
          "escaleras": [
            {
              "escalera": "1",
              "plantas": [
                {
                  "planta": "00",
                  "puertas": [
                    {
                      "puerta": "01",
                      "refCat": "9115501VK6891N0001GB",
                      "superficieVivienda": 60.0,
                      "superficieTotal": 60.0
                    }
                  ]
                },
                {
                  "planta": "01",
                  "puertas": [
                    {
                      "puerta": "01",
                      "refCat": "9115501VK6891N0002HZ",
                      "superficieVivienda": 154.0,
                      "superficieTotal": 170.0
                    }
                  ]
                },
                {
                  "planta": "02",
                  "puertas": [
                    {
                      "puerta": "01",
                      "refCat": "9115501VK6891N0003JX",
                      "superficieVivienda": 161.0,
                      "superficieTotal": 177.0
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "page": "1",
      "pagesTotal": "1",
      "status_code": "200",
      "status_text": "OK"
    };
alert(json.data[0].escaleras[0].plantas[0].puertas[0].refCat);




答案 1 :(得分:0)

您好,您必须遍历json对象才能获取数据。

使用以下代码。

module.exports = function(grunt) {

    //time
    require('time-grunt')(grunt);

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        sass: {
            dist: {
                options: {
                    sourceMap: true,
                    style: 'compressed',
                },

                files: {
                    'assets/css/style.css' : 'assets/scss/style.scss',
                }
            }
        },

        copy: {
            scripts: {
                cwd: 'bower_components/jquery/dist/',
                expand: true,
                src: [
                    'jquery.min.js',
                    ],
                dest: 'assets/js/',
                filter: 'isFile',
            }
        },

        concat: {
            options: {
                separator: ';',
            },
            js: {
                src: [
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/button.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/collapse.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/dropdown.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/modal.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/tab.js',
                    'bower_components/bootstrap-sass/assets/javascripts/bootstrap/transition.js',
                ],
                dest: 'assets/js/bootstrap.js',
            },
            sass: {
                src: [
                    'bower_components/bootstrap-sass/assets/stylesheets/_bootstrap.scss',
                    'bower_components/bootstrap-sass/assets/stylesheets/bootstrap/**/*.scss'
                ],
                dest: 'assets/scss/style.scss'
            }
        },

        uglify: {
            options: {
                mangle: false,
            },

            my_target: {
                files: {
                    'assets/css/style.min.css' : ['assets/css/style.css'],
                    'assets/js/bootstrap.min.js' : ['assets/js/bootstrap.js']
                }
            }
        },

        watch: {
            gruntfile: {
                files: 'Gruntfile.js',
                tasks: ['jshint:gruntfile'],
                options: {
                    livereload: true
                }
            },
            src: {
                files: ['assets/js/*.js', 'assets/scss/*.scss'],
                tasks: ['default'],
                options: {
                    livereload: true
                }
            }
        },

        jshint: {
            all: ['Gruntfile.js', 'assets/**/*.js']
        },


    });

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    grunt.registerTask('build', ['sass', 'copy', 'concat', 'uglify']);
    grunt.registerTask('default', ['watch', 'jshint']);
};

答案 2 :(得分:0)

假设你有一个id为'test'的div,这是有效的。我猜你的索引逻辑会产生'puertas'上的[i]不正确,或者你的'json'对象实际上不是JSON对象?

示例jsfiddle https://jsfiddle.net/p6udz7ao/

以下示例的副本:

$(document).ready(function () {
    var jsonString = '{ "request": { "operation": "Viviendas", "languague": "es" ,"idMunicipio":"5","idVia":"196","idProvincia":"28","pc1":"9115501","pc2":"VK6891N","numero":"1" }, "created_http": "Thu Oct 01 09:33:58 GMT+01:00 2015", "created_server": 20151001092458, "data": [ { "portal": "", "escaleras": [ { "escalera": "1", "plantas": [ { "planta": "00", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0001GB", "superficieVivienda": 60.0, "superficieTotal": 60.0 } ] } , { "planta": "01", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0002HZ", "superficieVivienda": 154.0, "superficieTotal": 170.0 } ] } , { "planta": "02", "puertas": [ { "puerta": "01", "refCat": "9115501VK6891N0003JX", "superficieVivienda": 161.0, "superficieTotal": 177.0 } ] } ] } ] } ], "page" : "1", "pagesTotal" : "1", "status_code" : "200", "status_text" : "OK" }';

    var jsonObject = JSON.parse(jsonString);
    var refcattext = jsonObject.data[0].escaleras[0].plantas[0].puertas[0].refCat;

    $('#test').text(refcattext);
});