javascript - JSON @属性

标签 javascript jquery xml json

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

Javascript:

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

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"
                    }
                }
            }
        ]
    }
}

错误(Chrome);

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

问题是,如何让“Kreuzlingen, Thurgovia”处于警报状态?

最佳答案

像这样:

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

关于javascript - JSON @属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11316923/

相关文章:

c# - 当前上下文中不存在名称 'ClientScript'

javascript - 指向捆绑在一起的 JS 模块的常见依赖关系的最佳方法是什么?

javascript - 当鼠标悬停在 <a> 标签元素中时,如何完全改变图像的颜色?

javascript - 如何使用 javascript/jQuery 将模型集合发送到 MVC Controller 或 View

xml - 使用基于 XML 的数据库有哪些优点和缺点?

javascript - 防止图像在移动设备上加载

javascript - 如何隐藏其余未使用的图像

javascript - 如何在 HTML 和 CSS 中实现具有可折叠组件的粘性划分?

html - 如何返回 XPath 表达式的单个字符串值?

php - Yii SOAP Web 服务返回 XML