搜索插件、代码仓与 APP 分类

插件详情v2.0.1

jmespath

jmespath 是 JMESPath 的 Dart 实现,JMESPath 是一种用于 JSON 的查询语言。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.4万
喜欢数
10
Pub 分数
130

安装插件

flutter pub add jmespath

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

jmespath 是 JMESPath 查询语言的 Dart 实现,旨在从 JSON 结构中提取和转换数据。它允许开发者编写表达性强的查询,以导航嵌套的 JSON 对象、过滤数组,并执行排序和映射等操作。非常适合解析 API 响应或配置文件,无需编写冗长代码即可简化复杂 JSON 数据的处理。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 查询 API 响应
  • 过滤和转换 JSON 数据
  • 解析配置文件
  • 从 JSON 中提取嵌套值
  • 简化应用中的 JSON 操作

主要特点

  • 表达性查询语法
  • 支持过滤、映射和排序
  • 跨平台兼容性
  • 轻量且快速
  • 适用于 JSON 导航的直观操作

适合对象

  • 使用 JSON API 的开发人员
  • 需要动态数据提取的应用程序
  • 配置处理工具
  • 处理JSON的后端服务

使用建议

  • 需要了解 JMESPath 语法
  • 不适用于非JSON数据
  • 仅限查询和转换,不包括验证或解析

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

jmespath - A JMESPath implementation in dart

jmespath is a dart implementation of JMESPath, which is a query language for JSON. It will take a JSON document and transform it into another JSON document through a JMESPath expression.

This project is derived from the go port of the JMESPath, go-jmespath

Using go-jmespath is really easy. There's a single function you use, jmespath.search:

> import 'package:jmespath/jmespath.dart';
>
> var jsondata = r'{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}';
> var data = json.decode(jsondata);
> var search_string = 'foo.bar.baz[2]';
> var result = search(search_string, data);
> print('example1 search $search_string , result = $result');
example1 search foo.bar.baz[2] , result = 2

In the example we gave the search function input data of {"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}} as well as the JMESPath expression foo.bar.baz[2], and the search function evaluated the expression against the input data to produce the result 2.

The JMESPath language can do a lot more than select an element from a list. Here are a few more examples:

> var jsondata = r'{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}';
> var data = json.decode(jsondata);
> var search_string = 'foo.bar';
> var result = search(search_string, data);
> print('example2 search $search_string , result = $result');
example2 search foo.bar , result = {baz: [0, 1, 2, 3, 4]}


> var jsondata =
>     r'{"foo": [{"first": "a", "last": "b"}, {"first": "c", "last": "d"}]}';
> var data = json.decode(jsondata);
> var search_string = 'foo[*].first';
> var result = search(search_string, data);
> print('example3 search $search_string , result = $result');
example3 search foo[*].first , result = [a, c]


> var jsondata =
>     r'{"foo": [{"age": 20}, {"age": 25}, {"age": 30}, {"age": 35}, {"age": 40}]}';
> var data = json.decode(jsondata);
> var search_string = 'foo[?age > `30`]';
> var result = search(search_string, data);
> print('example4 search $search_string , result = $result');
example4 search foo[?age > `30`] , result = [{age: 35}, {age: 40}]

You can also pre-compile your query. This is usefull if you are going to run multiple searches with it:

> var search_string = 'foo.bar';
> var jmespath = Jmespath.compile('foo.bar');

> var jsondata1 = r'{"foo": {"bar": "hello"}}';
> var data1 = json.decode(jsondata1);
> var result1 = jmespath.search(data1);
> print('example5 search $search_string , result for data1 = $result1');
example5 search foo.bar , result for data1 = hello

> var jsondata2 = r'{"foo": {"bar": "world"}}';
> var data2 = json.decode(jsondata2);
> var result2 = jmespath.search(data2);
> print('example5 search $search_string , result for data2 = $result2');
example5 search foo.bar , result for data2 = world

Exception Handling

All the functions in the library throw exception whose base class is JmesException. You can handle errors as follows :

> var jsondata = r'{"foo": {"bar": "hello"}}';
> var data = json.decode(jsondata);
> var search_string = 'avg(foo.bar)';
> try {
>   var result = search(search_string, data);
>   print('example6 search $search_string , result = $result');
> } on JmesException catch (e) {
>   print('example6 search $search_string , got exception ${e.message}');
> }
example6 search avg(foo.bar) , got exception Invalid type for hello, expected [jpType.jpArrayNumber]

More Resources

The example above only show a small amount of what a JMESPath expression can do. If you want to take a tour of the language, the best place to go is the JMESPath Tutorial.

One of the best things about JMESPath is that it is implemented in many different programming languages including python, ruby, php, lua, etc. To see a complete list of libraries, check out the JMESPath libraries page.

And finally, the full JMESPath specification can be found on the JMESPath site.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。