搜尋套件、儲存庫與 APP 分類

套件詳情v0.9.0

json_path

RFC 9535 的實作 - JSONPath:用於 JSON 的查詢表達式。使用類似 `$.store.book[2].price` 的查詢來讀取和寫入解析後的 JSON 物件中的值。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
41.3萬
喜歡數
126
Pub 分數
160

安裝套件

flutter pub add json_path

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

json_path 是一個 Dart 套件,實作了 RFC 9535 的 JSONPath,能強大地查詢與操作 JSON 資料。它允許使用者使用富有表現力的路徑表示式(例如 `$.store.book[2].price`)讀取和寫入已解析的 JSON 物件中的值。此套件支援標準的 JSONPath 語法,並可在 Android、iOS、Web、macOS、Windows 及 Linux 等多個平台相容使用。非常適合在 Flutter 應用中進行動態資料擷取與轉換。

適用情境

  • 提取嵌套的 JSON 值
  • 更新特定的 JSON 欄位
  • 在JSON資料中過濾陣列
  • 解析複雜的 JSON 結構
  • 動態配置處理
  • API 中的資料轉換

主要特色

  • 完全符合 RFC 9535
  • 支援讀取和寫入作業
  • 直覺的路徑語法,例如 `$.store.book[2].price`
  • 跨平台相容性
  • 輕量且快速
  • 強大的錯誤處理

適合對象

  • 需要動態JSON查詢的Flutter應用
  • 後端服務處理JSON資料負載
  • 組態管理系統
  • API 客戶端具有可變的回應結構
  • 需要靈活處理JSON的資料管道

使用建議

  • 需要有效的 JSON 輸入
  • 效能可能會因深度嵌套或大型 JSON 而異
  • 僅限 RFC 9535 中定義的 JSONPath 語法
  • 不適用於非JSON資料類型
  • 可能需要對不受信任的輸入進行仔細驗證

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

RFC 9535 - JSONPath: Query Expressions for JSON in Dart

Pub Package GitHub Issues GitHub Forks GitHub Stars GitHub License

JSONPath defines a string syntax for selecting and extracting JSON (RFC 8259) values from within a given JSON value.

This library is a Dart implementation of the RFC 9535 JsonPath specification. It is also expected to pass the latest version of the Compliance Test Suite. If you find a missing or incorrectly implemented feature, please open an issue.

For installation instructions and a detailed API documentation, see the pub.dev page.

Usage example:
import 'dart:convert';

import 'package:json_path/json_path.dart';

void main() {
  final json = jsonDecode('''
{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}  
  ''');

  final prices = JsonPath(r'$..price');

  print('All prices in the store:');

  /// The following code will print:
  ///
  /// $['store']['book'][0]['price']:	8.95
  /// $['store']['book'][1]['price']:	12.99
  /// $['store']['book'][2]['price']:	8.99
  /// $['store']['book'][3]['price']:	22.99
  /// $['store']['bicycle']['price']:	19.95
  prices
      .read(json)
      .map((match) => '${match.path}:\t${match.value}')
      .forEach(print);
}

Data manipulation

Each JsonPathMatch produced by the .read() method contains the .pointer property which is a valid JSON Pointer and can be used to alter the referenced value. If you only need to manipulate JSON data, check out my JSON Pointer implementation.

User-defined functions

The JSONPath parser may be extended with user-defined functions. The user-defined functions take precedence over the built-in ones specified by the standard. Currently, only functions of 1 and 2 arguments are supported.

To create your own function:

  1. Import package:json_path/fun_sdk.dart.
  2. Create a class implementing either Fun1 (1 argument) or Fun2 (2 arguments).

To use it:

  1. Create a new JsonPathParser with your function: final parser = JsonPathParser(functions: [MyFunction()]);
  2. Parse the expression: final jsonPath = parser.parse(r'$[?my_function(@)]');

For more details see the included example.

This package comes with a few non-standard functions which you might find useful.

  • count(<NodeList>) - returns the number of nodes selected by the argument
  • index(<SingularNodeList>) - returns the index under which the array element is referenced by the parent array
  • key(<SingularNodeList>) - returns the key under which the object element is referenced by the parent object
  • is_array(<Maybe>) - returns true if the value is an array
  • is_boolean(<Maybe>) - returns true if the value is a boolean
  • is_number(<Maybe>) - returns true if the value is a number
  • is_object(<Maybe>) - returns true if the value is an object
  • is_string(<Maybe>) - returns true if the value is a string
  • reverse(<Maybe>) - returns the reversed string
  • siblings(<NodeList>) - returns the siblings for the nodes
  • xor(<bool>, <bool>) - returns the XOR of two booleans arguments

To use them, import package:json_path/fun_extra.dart and pass the functions to the JsonPath() constructor:

final jsonPath = JsonPathParser(functions: [
  const Key(),
  const Reverse(),
]).parse(r'$[?key(@) == reverse(key(@))]');

References

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案