外部 JSON 数据无法在 Jekyll 中输出任何内容
external JSON data fails to output anything in Jekyll
我正在尝试将外部 JSON 数据提取到 Jekyll 中,但没有成功。
我发现 this code (link to a gist),这是另一个要点的分支...这只是添加了外部方法 ("from url")。
我试图将它变成我自己的标签插件(或者它们的名字),以简化它并可能解决一些问题:
_plugins/externaljson.rb
require 'json'
require 'net/http'
module ExternalJSON
class ExternalJSON_tag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def render(context)
if /(.+) from (.+)/.match(@text)
url = context[].strip
uri = URI( url )
response = Net::HTTP.get( uri )
data = JSON.parse( response )
context[] = JSON data
return ''
end
end
end
end
Liquid::Template.register_tag('externalJSON', ExternalJSON::ExternalJSON_tag)
...但我并没有真正解决所有问题或从中学到很多东西。我认为我学到的唯一一件事是,问题可能出在解析 ruby 中的文件和 jekyll 读取它之间的某个地方。
我运行这个测试使用上面的标签插件代码(↑):
---
layout: default
---
<!-- Using the code I modified -->
<!-- This capture exists to combine a string and a variable, but it's just a static url for the purposes of this example -->
{% capture myUrl %}
https://api.guildwars2.com/v2/recipes/2889
{% endcapture %}
{% externalJSON jsonData from myUrl %}
{% for data in jsonData %}
{{ data}}
{% endfor %}
<!-- Jekyll's native way of handling local data files -->
<!-- I just saved that json data from the url above(↑) locally for this one -->
{% for data in site.data.example %}
{{ data }}
{% endfor %}
这个测试让我意识到这两种方法输出的数据略有不同。
我的外部尝试:
{"type":"Meal","output_item_id":12210,"output_item_count":2,"min_rating":0,"time_to_craft_ms":1000,"disciplines":["Chef"],"flags":[],"ingredients":[{"item_id":24359,"count":1},{"item_id":12132,"count":1}],"id":2889,"chat_link":"[&CUkLAAA=]"}
Jekyll原生方法(本地文件)
{"type"=>"Meal", "output_item_id"=>12210, "output_item_count"=>2, "min_rating"=>0, "time_to_craft_ms"=>1000, "disciplines"=>["Chef"], "flags"=>[], "ingredients"=>[{"item_id"=>24359, "count"=>1}, {"item_id"=>12132, "count"=>1}], "id"=>2889, "chat_link"=>"[&CUkLAAA=]"}
如果我尝试做例如 {{ data.type }},我的外部尝试 returns 什么都没有,而 Jekyll 方法 returns 值就像它应该的那样。我只是不知道如何更改格式或丢失的部分。
我做错了什么?
将您的 render(context)
替换为以下内容:
def render(context)
if /(.+) from url (.+)/.match(@text)
resp = Net::HTTP.get_response(URI(.strip))
data = resp.body
context[] = JSON data
nil
else
# syntax error
raise ArgumentError, 'ERROR:bad_syntax'
end
end
然后像这样调用 data
:
{% externalJSON data from url http://foo.json %}
这将为您提供一个 data
对象,可以调用该对象来呈现各个键。
如果数据是数组,遍历元素并调用所需的键
{% for entry in data %}
{{ entry.type }}
{% endfor %}
如果数据是对象(hash),直接调用key
{{ data.type }}
我正在尝试将外部 JSON 数据提取到 Jekyll 中,但没有成功。
我发现 this code (link to a gist),这是另一个要点的分支...这只是添加了外部方法 ("from url")。
我试图将它变成我自己的标签插件(或者它们的名字),以简化它并可能解决一些问题:
_plugins/externaljson.rb
require 'json'
require 'net/http'
module ExternalJSON
class ExternalJSON_tag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def render(context)
if /(.+) from (.+)/.match(@text)
url = context[].strip
uri = URI( url )
response = Net::HTTP.get( uri )
data = JSON.parse( response )
context[] = JSON data
return ''
end
end
end
end
Liquid::Template.register_tag('externalJSON', ExternalJSON::ExternalJSON_tag)
...但我并没有真正解决所有问题或从中学到很多东西。我认为我学到的唯一一件事是,问题可能出在解析 ruby 中的文件和 jekyll 读取它之间的某个地方。
我运行这个测试使用上面的标签插件代码(↑):
---
layout: default
---
<!-- Using the code I modified -->
<!-- This capture exists to combine a string and a variable, but it's just a static url for the purposes of this example -->
{% capture myUrl %}
https://api.guildwars2.com/v2/recipes/2889
{% endcapture %}
{% externalJSON jsonData from myUrl %}
{% for data in jsonData %}
{{ data}}
{% endfor %}
<!-- Jekyll's native way of handling local data files -->
<!-- I just saved that json data from the url above(↑) locally for this one -->
{% for data in site.data.example %}
{{ data }}
{% endfor %}
这个测试让我意识到这两种方法输出的数据略有不同。
我的外部尝试:
{"type":"Meal","output_item_id":12210,"output_item_count":2,"min_rating":0,"time_to_craft_ms":1000,"disciplines":["Chef"],"flags":[],"ingredients":[{"item_id":24359,"count":1},{"item_id":12132,"count":1}],"id":2889,"chat_link":"[&CUkLAAA=]"}
Jekyll原生方法(本地文件)
{"type"=>"Meal", "output_item_id"=>12210, "output_item_count"=>2, "min_rating"=>0, "time_to_craft_ms"=>1000, "disciplines"=>["Chef"], "flags"=>[], "ingredients"=>[{"item_id"=>24359, "count"=>1}, {"item_id"=>12132, "count"=>1}], "id"=>2889, "chat_link"=>"[&CUkLAAA=]"}
如果我尝试做例如 {{ data.type }},我的外部尝试 returns 什么都没有,而 Jekyll 方法 returns 值就像它应该的那样。我只是不知道如何更改格式或丢失的部分。
我做错了什么?
将您的 render(context)
替换为以下内容:
def render(context)
if /(.+) from url (.+)/.match(@text)
resp = Net::HTTP.get_response(URI(.strip))
data = resp.body
context[] = JSON data
nil
else
# syntax error
raise ArgumentError, 'ERROR:bad_syntax'
end
end
然后像这样调用 data
:
{% externalJSON data from url http://foo.json %}
这将为您提供一个 data
对象,可以调用该对象来呈现各个键。
如果数据是数组,遍历元素并调用所需的键
{% for entry in data %}
{{ entry.type }}
{% endfor %}
如果数据是对象(hash),直接调用key
{{ data.type }}