使用 REST API 读取 JSON 数据
问题
一些网站提供了一个休息读取数据的 API,通常响应是 JSON 格式的文本。 我们如何将其纳入 JMP?
解决方案
这是一个使用meetup.com REST API . 它相对简单,因为它使用常见的“GET”方法,这是网页读取的默认方法。 更详细的消息使用“POST”方法,这需要不同的方法。 对于 GET 请求,输入会附加到 URL 本身。
my key = load text file("meetupkey.txt"); // I stored my key in a text file<BR />event id = "246678392"; // the meetup id at the end of the event's URL<BR /><BR />url = "https://api.meetup.com/2/rsvps?key=" || my key || "&event_id=" || event id;
s = load text file( url ); // s contains the JSON result as a big string<BR /><BR />// We could store that in a file and then open the file, but here's a shortcut.<BR />// The Open() function can read data directly from a blob -- you just have to tell it the file type
b = char to blob(s);
dt = open(b, "JSON");<BR />// JMP interprets the JSON structured tree into a flat data table, with surprisingly good results.
讨论
读取数据后,我经常做一些清理并创建方便的列,例如:
dt << New Column("reply time", formula(:results.mtime/1000 + 01Jan1970), format("ddmonyyyy"));
dt << New Column("count", formula(if(:results.response=="yes", :results.guests + 1, 0)));
在脚本中使用它可以轻松地根据需要刷新我的数据。 现在我准备做一些探索..
回复:使用 REST API 读取 JSON 数据有没有机会把它变成教程视频?
回复:使用 REST API 读取 JSON 数据极好的。
我将在 JMP 脚本部分发布更多 [具体] 示例。
-布莱恩
回复:使用 REST API 读取 JSON 数据嗨布莱恩,
非常感谢。我能够成功地发送和接收表单,并将尝试将其应用到我的脚本中。
干杯,
霍华德
回复:使用 REST API 读取 JSON 数据
嘿霍华德,
通常,当您使用“POST”HTTP 方法时,您是在“发布”-ing 表单数据。
确切的方法由 Web 服务的 RestAPI 定义,因为它们可以将“POST”、“PUT”、“GET”等解释为暗示某些事情。 (像这样: https://www.w3schools.com/tags/ref_httpmethods.asp )
当您使用 HTTP 请求时(如您的示例所示),查询字符串将成为 url 的一部分(例如:
“ http://httpbin.org/post?key1=value1&key2=value2 ")
在你的例子中,如果你想发布数据,你会这样做:
url = "http://httpbin.org/post";
request = New HTTP Request(
URL( url ),
Method( "Post" ),
Form(
Fields([[
"key1" => "value1", "key2" => "value2"
]])
)
);
json = request << Send();
jsl_json = Parse JSON( json );
Write( jsl_json );
查询字符串被替换为表单。
Form里面有两个item,Fields和Files,都是Associative Arrays。
或者,您可以像这样编写代码:
url = "http://httpbin.org/post";
my_fields = [=>];
for(i = 1, i <= 2, i++,
my_fields["key" || Char(i)] = "value" || Char(i);
);
request = New HTTP Request(
URL( url ),
Method( "Post" ),
Form(
Fields(my_fields)
)
);
json = request << Send();
jsl_json = Parse JSON( json );
Write( jsl_json );
取决于您要如何为表单创建键/值对。
-布莱恩
回复:使用 REST API 读取 JSON 数据我正在尝试使用格式为 JSON 的表格数据提交 POST 请求。以下代码以输入作为 args 回显 POST 请求,类似于 GET 请求。
url = "http://httpbin.org/post";
request = New HTTP Request(
URL( url ),
Method( "Post" ),
Query String(
[[
"key1" => "value1", "key2" => "value2"
]]
)
);
json = request << Send();
jsl_json = Parse JSON( json );
Write( jsl_json );
但是,我想使用 JSON 格式的数据进行 POST。以下是我如何从 Mac OSX 终端执行此操作:
curl -d '{"table1":[{"id": 1,"value1": 10,"value2": 11},{"id": 2,"value1": 20,"value2": 21}],"table2":[{"id": 3,"value1": 30,"value2": 31},{"id": 4,"value1": 40,"value2": 41}]}' -H "Content-Type: application/json" -X POST http://httpbin.org/post
我怀疑我需要替换“查询字符串”,但文档不清楚用什么替换它。任何指导将不胜感激。
在此先感谢!
霍华德
回复:使用 REST API 读取 JSON 数据凉爽的!
这比我使用的代码更容易这里. 在 JMP 13 中,它可以很简单
Open( "http://api.open-notify.org/iss-now.json?r=" || Char( Random Integer( 1, 1e6 ) ), "JSON" );
International Space Station positionInternational Space Station position国际空间站位置
请注意随机数技巧:URL 每次看起来都不一样,因此您不会获得旧的、陈旧的、缓存的数据副本。 末尾的 ?r= 启动一个名为 r 的参数。 这个网站不需要任何。 如果有命名参数,例如“?time=3:00&place=space”,您可以添加网站将忽略的自己的参数名称: "?time=3:00&place=space&r="(在这里加上随机数)。
在 JMP 12 中,您将获得 JSON 字符串:“{\!”iss_position\! {\!“纬度\!”: \!"-41.6934\!", \!“经度\!”: \!"-140.2738\!"}, \!"时间戳\!": 1518704338, \"消息\!": \!“成功\!”}”