javascript JSON @attributes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11316923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:44:39  来源:igfitidea点击:

JSON @attributes

javascriptjqueryxmljson

提问by enloz

I have a hard time to understand how to read JSON object containing "@attributes".

我很难理解如何读取包含“@attributes”的 JSON 对象。

Javascript:

Javascript:

$.ajax({
    type: "GET",
    dataType: 'json',
    url: "http://..../script/weather.php?r="+req,
    success: function(data){
       alert(data.weather.forecast_information.city[0].data)
    }
});

JSONresponse:

JSON响应:

{
    "@attributes": {
        "version": "1"
    },
    "weather": {
        "@attributes": {
            "module_id": "0",
            "tab_id": "0",
            "mobile_row": "0",
            "mobile_zipped": "1",
            "row": "0",
            "section": "0"
        },
        "forecast_information": {
            "city": {
                "@attributes": {
                    "data": "Kreuzlingen, Thurgovia"
                }
            },
            "postal_code": {
                "@attributes": {
                    "data": "kreuzlingen"
                }
            },
            "latitude_e6": {
                "@attributes": {
                    "data": ""
                }
            },
            "longitude_e6": {
                "@attributes": {
                    "data": ""
                }
            },
            "forecast_date": {
                "@attributes": {
                    "data": "2012-07-03"
                }
            },
            "current_date_time": {
                "@attributes": {
                    "data": "1970-01-01 00:00:00 +0000"
                }
            },
            "unit_system": {
                "@attributes": {
                    "data": "US"
                }
            }
        },
        "current_conditions": {
            "condition": {
                "@attributes": {
                    "data": "Cloudy"
                }
            },
            "temp_f": {
                "@attributes": {
                    "data": "70"
                }
            },
            "temp_c": {
                "@attributes": {
                    "data": "21"
                }
            },
            "humidity": {
                "@attributes": {
                    "data": "Humidity: 68%"
                }
            },
            "icon": {
                "@attributes": {
                    "data": "/ig/images/weather/cloudy.gif"
                }
            },
            "wind_condition": {
                "@attributes": {
                    "data": "Wind: N at 0 mph"
                }
            }
        },
        "forecast_conditions": [
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Tue"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "55"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "72"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/thunderstorm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Thunderstorm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Wed"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "66"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "79"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_storm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Storm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Thu"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "61"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "77"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_storm.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Storm"
                    }
                }
            },
            {
                "day_of_week": {
                    "@attributes": {
                        "data": "Fri"
                    }
                },
                "low": {
                    "@attributes": {
                        "data": "63"
                    }
                },
                "high": {
                    "@attributes": {
                        "data": "79"
                    }
                },
                "icon": {
                    "@attributes": {
                        "data": "/ig/images/weather/chance_of_rain.gif"
                    }
                },
                "condition": {
                    "@attributes": {
                        "data": "Chance of Rain"
                    }
                }
            }
        ]
    }
}


ERROR (Chrome);

错误(铬);

Uncaught TypeError: Cannot read property 'data' of undefined

未捕获的类型错误:无法读取未定义的属性“数据”

Question is, how to get "Kreuzlingen, Thurgovia" in alert ?

问题是,如何让“克罗伊茨林根,图尔戈维亚”处于警戒状态?

回答by Engineer

Get like this:

得到这样的:

alert(data.weather.forecast_information.city["@attributes"].data)