{"id":37034195,"url":"https://github.com/cuture/cuture.extensions.systemtextjson.dynamic","last_synced_at":"2026-01-14T04:00:47.945Z","repository":{"id":65807283,"uuid":"600335220","full_name":"cuture/Cuture.Extensions.SystemTextJson.Dynamic","owner":"cuture","description":"Extension of `System.Text.Json` to support dynamic access using `dynamic`; 对 `System.Text.Json` 的拓展，以支持使用 `dynamic` 进行动态访问","archived":false,"fork":false,"pushed_at":"2025-08-10T02:46:02.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-17T22:50:12.500Z","etag":null,"topics":["dotnet","dynamic","dynamic-json","dynamic-system-text-json","json","system-text-json"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cuture.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-02-11T06:53:06.000Z","updated_at":"2025-08-10T02:45:53.000Z","dependencies_parsed_at":"2023-06-03T03:45:32.851Z","dependency_job_id":null,"html_url":"https://github.com/cuture/Cuture.Extensions.SystemTextJson.Dynamic","commit_stats":{"total_commits":5,"total_committers":2,"mean_commits":2.5,"dds":"0.19999999999999996","last_synced_commit":"95e952349a9187d674275374a3023a6ae58e6c1f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/cuture/Cuture.Extensions.SystemTextJson.Dynamic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuture%2FCuture.Extensions.SystemTextJson.Dynamic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuture%2FCuture.Extensions.SystemTextJson.Dynamic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuture%2FCuture.Extensions.SystemTextJson.Dynamic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuture%2FCuture.Extensions.SystemTextJson.Dynamic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cuture","download_url":"https://codeload.github.com/cuture/Cuture.Extensions.SystemTextJson.Dynamic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cuture%2FCuture.Extensions.SystemTextJson.Dynamic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","dynamic","dynamic-json","dynamic-system-text-json","json","system-text-json"],"created_at":"2026-01-14T04:00:47.248Z","updated_at":"2026-01-14T04:00:47.932Z","avatar_url":"https://github.com/cuture.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# Cuture.Extensions.SystemTextJson.Dynamic\n\nExtension of `System.Text.Json` to support dynamic access using `dynamic`\n\n对 `System.Text.Json` 的拓展，以支持使用 `dynamic` 进行动态访问\n\n - 使用 `dynamic` 访问，无需预定义实体类型\n - 支持修改/添加属性\n\n## 创建 `JSON` 对象\n\n### 从 `Json字符串` 创建 `JSON` 对象\n```C#\nusing System.Text.Json.Dynamic;\n\nvar rawJson = \n    \"\"\"\n    {\n    \"a\":1,\n    \"b\":2.0,\n    \"c\":{\n        \"a\":\"1\",\n        \"b\":\"2\",\n        },\n    }\n    \"\"\";\n\nvar json = JSON.parse(rawJson);\n```\n\n### 从 `对象` 创建 `JSON` 对象\n```C#\nusing System.Text.Json.Dynamic;\n\nvar json = JSON.create(new object());\n```\n\n## 访问 `JSON` 对象\n\n```C#\n//访问属性\nvar a = json.a;\nvar c = json.a.b.c;\n\n//访问数组\nvar v2 = json.x[1];\nvar v3 = json.x.last();\nvar v4 = json.x.first();\nvar v5 = json.x.push(obj);\nvar v6 = json.x.pop();\nvar v7 = json.x.add(obj);\nvar v8 = json.x.append(obj);\nvar v9 = json.x.insert(1,obj);\nvar va = json.x.RemoveAt(1);\njson.x.clear();\n\n//访问属性并转换为基础数据类型\nbool b1 = json.b;\nstring s1 = json.s;\n\n//局部反序列化\nMyClass obj = json.a.b; //将 json.a.b 反序列化到类型 MyClass\n\n//获取内部的原始 System.Text.Json.Nodes.* 对象\nJsonArray array = json.ArrayProperty;\nJsonObject obj = json.ObjectProperty;\nJsonNode node = json.NodeProperty;\n```\n\n## 修改 `JSON` 对象\n\n```C#\n//设置值（不可多级设置未定义字段）\njson.a = new {};\njson.a.b = 1;\n\njson.c = new\n{\n    d = new\n    {\n        e = 2\n    }\n};\n\nint value = json.a.b;\n```\n\n## 检查 `JSON` 对象\n\n```C#\n//判断是否为未定义字段\nvar b1 = json.notexistfield == null;\nvar b2 = json.notexistfield == JSON.Undefined;\nvar b3 = JSON.isUndefined(json.notexistfield);\n\n//多级属性判断是否为未定义字段\nvar b4 = JSON.isUndefined(() =\u003e json.a.b.c.e.f.g.h.i.j.k);\n```\n\n## IEnumerable\n\n使用`IEnumerable`以支持使用Linq, (C#不支持实现dynamic的接口，所以需要额外的转换)\n\n```C#\n//遍历Array\n//显式赋值类型\nIEnumerable\u003cdynamic\u003e enumerable = json.Array;\n//通过IDynamicEnumerable\nvar enumerable = ((IDynamicEnumerable)json.Array).AsEnumerable();\n\n//遍历属性\n//显式赋值类型\nIEnumerable\u003cKeyValuePair\u003cstring, dynamic?\u003e\u003e enumerable = json;\n//通过IDynamicKeyValueEnumerable\nvar enumerable = ((IDynamicKeyValueEnumerable)json).AsEnumerable();\n```\n\n## Index \u0026\u0026 Range\n\n支持使用 Index 和 Range 语法访问数组\n\nNote: 当前使用 Range 语法将会创建新的对象，对其修改不会反映到原始对象\n\n```C#\n//Index\nvar value = json.Array[^1];\n\n//Range\nvar items = json.Array[..1];\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuture%2Fcuture.extensions.systemtextjson.dynamic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcuture%2Fcuture.extensions.systemtextjson.dynamic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcuture%2Fcuture.extensions.systemtextjson.dynamic/lists"}