学习unigui【27】像pg的jsonb一样编辑json。
1.unidbgrid的rowedit=true2.unigui的demo-\Demos\Desktop\DBAppDemo\SimpleDemo.dproj【11】3.unigui的session【1】4.unigui菜单【3】5.unigui的ini文件读写【6】6.unigui导出TMS.Flexcel【5】7.unigui+fastreport 打印【4】8.unigui显示uniTreeVview使用TUniTreeNode内存泄漏的问题【14】9.unigui显示websocket服务端向客户端发送信息【15】10.学习unigui【17】-数据集和JSON互相转换-DataSetConverter4D 开源项目11.学习unigui【18】unidbgrid的GridsGroupingSorting12.学习unigui【19】unidbgrid的Group By This Field汉化13.学习unigui【20】unistringGrid14.学习unigui【21】unistringGrid的标题栏动态增加15.学习unigui【22】unistringGrid的标题栏双击事件16.学习unigui【23】uniDBGrid的使用摘要17.学习unigui【25】关于图标18.学习unigui【24】Echart的使用:多个坐标系19.学习unigui【26】关于Unigui控件的个性化CSS框架
20.学习unigui【27】像pg的jsonb一样编辑json。
21.unigui的菜单树补习【2】treeview22.unigui的程序编译后自动运行傻傻的手动【7】23.unigui的ServerModule的重要属性【8】24.unigui的错误delphi clientHeight:property clientheight does not exist【10】25.学习unigui【28】UniGUI接收POST/GETvar
I: Integer;
CurrentObject: TJSONObject;
FieldName: string;
Pair: TJSONPair;
function CreateJSONValueForVariant(const aValue: Variant): TJSONValue;
begin
case VarType(aValue) of
varInteger:
Result := TJSONNumber.Create(Integer(aValue)); // 直接为整数创建TJSONNumber
varSingle, varDouble:
Result := TJSONNumber.Create(Double(aValue)); // 对于单精度和双精度浮点数,直接传入Value
varString:
Result := TJSONString.Create(string(aValue));
varBoolean:
Result := TJSONBool.Create(aValue);
else
raise Exception.CreateFmt('Unsupported variant type encountered: %s', [VarToStr(aValue)]); // 更详细的错误信息
end;
end;
begin
CurrentObject := JsonObject;
for I := Low(FieldNames) to High(FieldNames) - 1 do
begin
FieldName := FieldNames[I];
if not CurrentObject.TryGetValue(FieldName, CurrentObject) or not (CurrentObject is TJSONObject) then
begin
// 如果路径段不存在或不是对象,则创建一个新的对象
CurrentObject.AddPair(FieldName, TJSONObject.Create);
CurrentObject := CurrentObject.Values[FieldName] as TJSONObject;
end
else
begin
// 继续深入到下一个路径段
// CurrentObject := CurrentObject.Values[FieldName] as TJSONObject;
if CurrentObject.Values[FieldName] is TJSONObject then
begin
CurrentObject := CurrentObject.Values[FieldName] as TJSONObject;
end
else
begin
// 如果不是对象,则不应该继续遍历,直接跳出循环
Break;
end;
end;
end;
// 到达最后一级路径,设置值
if Assigned(CurrentObject) then
begin
FieldName := FieldNames[High(FieldNames)];
Pair := CurrentObject.Get(FieldName);
if Pair = nil then
CurrentObject.AddPair(FieldName, CreateJSONValueForVariant(NewValue)) // 自动根据NewValue类型创建JSONValue
else
Pair.JsonValue := CreateJSONValueForVariant(NewValue);
end;
Result := JsonObject;
end;
调用: var JsonObject : TJSONObject; JsonObject := TJSONObject.ParseJSONValue('{}') as TJSONObject; JsonObject :=TSQLHelp.UpdateNestedJSONField(JsonObject, ['person', 'age'], Integer(30));//注意这里,否则不识别导致异常 ShowMessage(JsonObject.ToString);
根据path:['person', 'age']检查json,有就更新,没有就创建。是否非常方便!
json有值,就修改:
var JsonObject : TJSONObject; JsonObject := TJSONObject.ParseJSONValue('{"person":{"name":"John","age":90,"address":{"city":"New York","country":"USA"}}}') as TJSONObject; JsonObject :=TSQLHelp.UpdateNestedJSONField(JsonObject, ['person', 'age'], Integer(30)); ShowMessage(JsonObject.ToString);
var JsonObject : TJSONObject; JsonObject := TJSONObject.ParseJSONValue('{"person":{"name":"John","age":90,"address":{"city":"New York","country":"USA"}}}') as TJSONObject; JsonObject :=TSQLHelp.UpdateNestedJSONField(JsonObject, ['person', 'age'], Integer(30)); JsonObject :=TSQLHelp.UpdateNestedJSONField(JsonObject, ['接触暴露', '皮肤','破损'], True); ShowMessage(JsonObject.ToString);
have fun
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
2020-06-03 FireDAC 下FDMEMTable的的字段自动获取