如何从外部类访问JSON响应 - Javascript

时间:2016-01-11 16:29:34

标签: json response

我知道这应该很简单,但我是一个项目的新手,并且无法弄清楚如何获得这个值。我已经尝试过#34;,&#34; this.snippet&#34;,message.whatever等。仅举例来说,我如何从响应中获取properties.network_id?< / p>

呼叫者:

v1.value.getSnippet(topology).then(function() {
        console.log("snippet ->: " + JSON.stringify());
        //delete topology.extraData;
    });

致电:

 getSnippet: function(topology) {
        if (!this.snippet) {
            if (!this.promise) {
                var self = this;
                this.promise =  xhr(uReleaseConfig.urls.rest + "/cloud/network/" + this.id, {
                    headers: {
                        "Accept": "application/json"
                    },
                    handleAs: "text"
                }).then(function (response) {
                    var data = JSON.parse(response || null);
                    self.snippet = data.message;
                }, function(error){
                    self.error = error;
                 });
            }
            return this.promise;
        }
        return this.snippet;
    }

响应:

{"message":"{\"__name_placeholder__\":{\"type\": \"OS::Neutron::Net\", \"properties\": {\"network_id
\": \"b2ac2526-ae8f-4d82-b1da-8d77b79f3476\", \"external\":\"false\", \"subnets\": [{\"id\":\"a1e3441e-4dcc-4c4a-ac12-2b60fe440c51
\",\"name\":\"product-dev-subnet-ben\",\"cidr\":\"192.168.20.0\\\/24\"},{\"id\":\"e2e05590-5539-45bb-995e-8f565e6c16ff
\",\"name\":\"product-dev-subnet\",\"cidr\":\"192.168.10.0\\\/24\"}] }}}\n"}

1 个答案:

答案 0 :(得分:0)

我在返回值中需要一个var:

.then(function(returnVal)

console.log("snippet ->: " + JSON.stringify(returnVal));