我很难理解如何读取包含“@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/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("