搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目