搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。