I am trying to read individual data from the content of json API on Oil and Gas Authority website; however the code I have returns all the data. Any help would be highly appreciated.
我正在尝试从Oil and Gas Authority网站json API内容中读取个人数据;但是我的代码返回所有的数据。如有任何帮助,我们将不胜感激。
require 'json'
require 'open-uri'
def index
url='http://data-ogauthority.opendata.arcgis.com/datasets/ab4f6b9519794522aa6ffa6c31617bf8_0.geojson'
@result = JSON.parse open(url).read
end
This my index view:
这我的索引视图:
<% @result.each do |row| %>
<%= row %>
<% end %>
3 个解决方案
#1
3
Given that the API (as you are currently using it) returns a JSON structure like this:
假设API(正如您当前使用的那样)返回一个JSON结构,如下所示:
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"FIELDNAME":"GRYPHON",
"FIELDTYPE":"OIL",
"NAME_SHORT":"GRYPHON",
"STATUS":"PRODUCING",
"DISC_DATE":"1987/07",
"DISC_WELL":"9/18b-7",
"STAT_CODE":"800",
"PROD_DATE":"1993/10",
"DEPTH_M":"111.86",
"DET_STATUS":"DETERMINED",
"ORIG_OP":"KERR-MCGEE",
"ORIG_LIC":"P.496",
"ORIG_LIC_2":"P.478",
"ORIG_LIC_3":"P.257",
"ORIG_LIC_4":"P.103",
"ORIG_LIC_5":" ",
"CURR_OPER":"MAERSK OIL NORTH SEA UK LIMITED",
"FIELDDATA":"https://itportal.decc.gov.uk/fields/fields_index/ukcs+field+information+listed+by+field+name/183.htm",
"OBJECTID":16,
"OGA_COP":null
},
"geometry":{
"type":"Polygon",
"coordinates":[
[
[1.5701447246411744,59.35253688325039],
...
]
]
}
},
...
]
}
You could do something like:
你可以这样做:
<% @result[:features].each do |feature| %>
<%= feature[:properties][:FIELDNAME] %>
<%= feature[:properties][:FIELDTYPE] %>
...
<% end %>
Your JSON file looks to be something like 1.3MB. So, unless you can figure out how to filter your results on the API side (using query params, I would suppose), you may end up with various performance issues in retrieving the JSON.
您的JSON文件看起来像1.3MB。因此,除非您知道如何在API端过滤结果(我想应该是使用查询解析),否则在检索JSON时可能会遇到各种性能问题。
And, you may want to do:
你可能会想:
@result = JSON.parse(open(url).read).with_indifferent_access
So that you can use symbols
to access hash
elements as well as strings
.
这样你就可以使用符号来访问哈希元素和字符串。
#2
1
One thing to add to @jvillian answer is that if one of the keys is nil
then calling this branch's subsequent keys will result in undefined method '[]'
. Ruby 2.3+ has a new method called dig
which will simply return nil
. More on dig
in my answer to this question.
@jvillian回答的一个问题是,如果其中一个键为nil,那么调用该分支的后续键将导致未定义的方法[]。Ruby 2.3+有一个新方法叫做dig,它只返回nil。更多关于我对这个问题的回答。
#3
0
Also to add to @jvillian answer, you can fetch the filtered information using this link; for example, taking into account the fields you need:
还要添加到@jvillian答案,您可以使用这个链接获取经过过滤的信息;例如,考虑到你需要的领域:
- FIELDNAME
- 字段名
- FIELDTYPE
- FIELDTYPE
- STATUS
- 状态
- DISC_DATE
- DISC_DATE
- DISC_WELL
- DISC_WELL
You could create query that will result in the following response:
您可以创建将导致以下响应的查询:
{
"displayFieldName": "FIELDNAME",
"fieldAliases": {
"FIELDNAME": "Field Name",
"FIELDTYPE": "Field Type",
"STATUS": "Status",
"DISC_DATE": "Discovery Date",
"DISC_WELL": "Discovery Well"
},
"fields": [
{
"name": "FIELDNAME",
"type": "esriFieldTypeString",
"alias": "Field Name",
"length": 32
},
{
"name": "FIELDTYPE",
"type": "esriFieldTypeString",
"alias": "Field Type",
"length": 4
},
{
"name": "STATUS",
"type": "esriFieldTypeString",
"alias": "Status",
"length": 50
},
{
"name": "DISC_DATE",
"type": "esriFieldTypeString",
"alias": "Discovery Date",
"length": 20
},
{
"name": "DISC_WELL",
"type": "esriFieldTypeString",
"alias": "Discovery Well",
"length": 20
}
],
"features": [
{
"attributes": {
"FIELDNAME": "GRYPHON",
"FIELDTYPE": "OIL",
"STATUS": "PRODUCING",
"DISC_DATE": "1987/07",
"DISC_WELL": "9/18b-7"
}
},
{
"attributes": {
"FIELDNAME": "BRAEMAR",
"FIELDTYPE": "COND",
"STATUS": "PRODUCING",
"DISC_DATE": "1995/05",
"DISC_WELL": "16/03b-8Z"
}
},
...
]
}
This file will be now 76KB, and you can extract the data in almost the same way, just change properties
for attributes
, i.e.:
这个文件现在将是76KB,您可以以几乎相同的方式提取数据,只需更改属性的属性,例如:
<% @result[:features].each do |feature| %>
<%= feature[:attributes][:FIELDNAME] %>
<%= feature[:attributes][:FIELDTYPE] %>
...
<% end %>
#1
3
Given that the API (as you are currently using it) returns a JSON structure like this:
假设API(正如您当前使用的那样)返回一个JSON结构,如下所示:
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"FIELDNAME":"GRYPHON",
"FIELDTYPE":"OIL",
"NAME_SHORT":"GRYPHON",
"STATUS":"PRODUCING",
"DISC_DATE":"1987/07",
"DISC_WELL":"9/18b-7",
"STAT_CODE":"800",
"PROD_DATE":"1993/10",
"DEPTH_M":"111.86",
"DET_STATUS":"DETERMINED",
"ORIG_OP":"KERR-MCGEE",
"ORIG_LIC":"P.496",
"ORIG_LIC_2":"P.478",
"ORIG_LIC_3":"P.257",
"ORIG_LIC_4":"P.103",
"ORIG_LIC_5":" ",
"CURR_OPER":"MAERSK OIL NORTH SEA UK LIMITED",
"FIELDDATA":"https://itportal.decc.gov.uk/fields/fields_index/ukcs+field+information+listed+by+field+name/183.htm",
"OBJECTID":16,
"OGA_COP":null
},
"geometry":{
"type":"Polygon",
"coordinates":[
[
[1.5701447246411744,59.35253688325039],
...
]
]
}
},
...
]
}
You could do something like:
你可以这样做:
<% @result[:features].each do |feature| %>
<%= feature[:properties][:FIELDNAME] %>
<%= feature[:properties][:FIELDTYPE] %>
...
<% end %>
Your JSON file looks to be something like 1.3MB. So, unless you can figure out how to filter your results on the API side (using query params, I would suppose), you may end up with various performance issues in retrieving the JSON.
您的JSON文件看起来像1.3MB。因此,除非您知道如何在API端过滤结果(我想应该是使用查询解析),否则在检索JSON时可能会遇到各种性能问题。
And, you may want to do:
你可能会想:
@result = JSON.parse(open(url).read).with_indifferent_access
So that you can use symbols
to access hash
elements as well as strings
.
这样你就可以使用符号来访问哈希元素和字符串。
#2
1
One thing to add to @jvillian answer is that if one of the keys is nil
then calling this branch's subsequent keys will result in undefined method '[]'
. Ruby 2.3+ has a new method called dig
which will simply return nil
. More on dig
in my answer to this question.
@jvillian回答的一个问题是,如果其中一个键为nil,那么调用该分支的后续键将导致未定义的方法[]。Ruby 2.3+有一个新方法叫做dig,它只返回nil。更多关于我对这个问题的回答。
#3
0
Also to add to @jvillian answer, you can fetch the filtered information using this link; for example, taking into account the fields you need:
还要添加到@jvillian答案,您可以使用这个链接获取经过过滤的信息;例如,考虑到你需要的领域:
- FIELDNAME
- 字段名
- FIELDTYPE
- FIELDTYPE
- STATUS
- 状态
- DISC_DATE
- DISC_DATE
- DISC_WELL
- DISC_WELL
You could create query that will result in the following response:
您可以创建将导致以下响应的查询:
{
"displayFieldName": "FIELDNAME",
"fieldAliases": {
"FIELDNAME": "Field Name",
"FIELDTYPE": "Field Type",
"STATUS": "Status",
"DISC_DATE": "Discovery Date",
"DISC_WELL": "Discovery Well"
},
"fields": [
{
"name": "FIELDNAME",
"type": "esriFieldTypeString",
"alias": "Field Name",
"length": 32
},
{
"name": "FIELDTYPE",
"type": "esriFieldTypeString",
"alias": "Field Type",
"length": 4
},
{
"name": "STATUS",
"type": "esriFieldTypeString",
"alias": "Status",
"length": 50
},
{
"name": "DISC_DATE",
"type": "esriFieldTypeString",
"alias": "Discovery Date",
"length": 20
},
{
"name": "DISC_WELL",
"type": "esriFieldTypeString",
"alias": "Discovery Well",
"length": 20
}
],
"features": [
{
"attributes": {
"FIELDNAME": "GRYPHON",
"FIELDTYPE": "OIL",
"STATUS": "PRODUCING",
"DISC_DATE": "1987/07",
"DISC_WELL": "9/18b-7"
}
},
{
"attributes": {
"FIELDNAME": "BRAEMAR",
"FIELDTYPE": "COND",
"STATUS": "PRODUCING",
"DISC_DATE": "1995/05",
"DISC_WELL": "16/03b-8Z"
}
},
...
]
}
This file will be now 76KB, and you can extract the data in almost the same way, just change properties
for attributes
, i.e.:
这个文件现在将是76KB,您可以以几乎相同的方式提取数据,只需更改属性的属性,例如:
<% @result[:features].each do |feature| %>
<%= feature[:attributes][:FIELDNAME] %>
<%= feature[:attributes][:FIELDTYPE] %>
...
<% end %>