use Test::Nginx::Socket 'no_plan';
use JSON;
run_tests();
sub j {
my @keys = @_;
return sub {
my ($resp) = @_;
my $result = decode_json( $resp );
my @values = ();
foreach my $key (@keys) {
push @values, $result->{$key};
}
return join("|", @values);
}
}
__DATA__
=== TEST 1: just test json
--- config
location = /1 {
content_by_lua 'ngx.say([==[{"a":123,"b":456,"c":789}]==])';
}
--- request
GET /1
--- response_body_filters eval
::j("a", "c", "b")
--- response_body: 123|789|456
目前没有处理嵌套的 JSON 属性比如 "a.b"等等,如果你有这种需求,再改改就好
在 2016年9月22日星期四 UTC+8下午3:39:15,WenMing写道:
一个 api 的返回值格式为 json, 我想判断其中某一两个 key 的值。请问有现成的方法吗?还是需要自己写 lua 或者 perl 的代码?