目前web开发中融入ajax元素带来一些优点,如不刷新界面传递数据。ajax的数据传递又以json格式为流行,因为很多流行的UI控件支持或要求json格式传输的数据,如ztree、easyui、jqgaid等。如果在你的domino web开发中还没有使用json传输方法,就请找相关资料学习(本人的csdn上传有demo)。
在domino的视图列显示json格式,简化ls,java,ssjs调用时生成json格式,适应前台各种控件的json格式需要。这里是显示{“name”:”value”},{…….},想要 [{},….]等其它格式 ,自己更改。
为什么要在domino视图直接转json格式,而不在代理ls、java、xagent(ssjs中生成的)拼写json格式呢?本质就是快,请看这篇测试文章 http://www.lotuschina.net/club/thread-3377-1-1.html
*1)如果域多时(网上找到的),把不想要的域放到_exclude里面
原文:http://openntf.org/XSnippets.nsf/snippet.xsp?id=use-transform-to-build-json-and-consume-the-output-in-an-xagent
_exclude:="$FILE":"$Fonts":"Form":"$UpdatedBy":"$Revisions":"ID":"ModifiedBy":"AddressInvoiceAppartment";
_fld:=@Trim(@ReplaceSubstring(@DocFields;_exclude;@Nothing));
"{\"@unid\":\""
+@Text(@DocumentUniqueID)+"\","
+ @Implode (
@Transform (
_fld; "_fn" ; "\"" +_fn + "\":\"" + @Text ( @GetField ( _fn) ) +"\"" ) ; "," ) +
"},"
*2)如果域少时, (根据1)更改的)
_fld:="F_riqi":"F_shijian":"F_shoufeizhan";
"{"+ @Implode (@Transform (_fld;"_fn" ; "\"" + _fn + "\":\"" +@Text ( @GetField ( _fn) ) + "\"" ) ; "," )+",\"@unid\":\""+@Text(@DocumentUniqueID)+"\"},"
*3)这个解决了包括特殊符号的情况
_fld:="code":"Quantity":"ITEMNAME";
json:="";
m := 1;
@While(m <= @Elements(_fld);
temp:=@Text(@Implode(@Trim(@GetField(_fld[m])); ","));
x:="";
n :=0;
@While(n<= @Length(temp);
@Do(
y:=@Middle(temp;n;1);
x:=x+@If(y=@Char(8);"\\"+@Char(8);
y=@Char(9);"\\"+@Char(9);
y=@Char(10);"\\"+@Char(10);
y=@Char(12);"\\"+@Char(12);
y=@Char(13);"\\"+@Char(13);
y=@Char(34);"\\"+@Char(34);
y=@Char(39);"\\"+@Char(39);
y=@Char(47);"\\"+@Char(47);
y=@Char(92);"\\"+@Char(92);
y)
);
n :=n + 1);
json:=json+"\""+_fld[m]+"\":\""+x+"\",";
m := m + 1);
"{"+json+"\"UID\":\""+@Text(@DocumentUniqueID)+"\"}"
*4)针对3)再次简化,这个已经够简单了吧
_fld:="code":"Quantity":"ITEMNAME";
json:="";
m := 1;
@While(m <= @Elements(_fld);
temp:=@Text(@Trim(@GetField(_fld[m])));
temp:=@ReplaceSubstring(temp ; @Char(92) ; "\\"+@Char(92) );
temp:=@ReplaceSubstring(temp ; @Char(47) ; "\\"+@Char(47) );
temp:=@ReplaceSubstring(temp ; @Char(39) ; "\\"+@Char(39) );
temp:=@ReplaceSubstring(temp ; @Char(8) ;"\\"+@Char(8) );
temp:=@ReplaceSubstring(temp ; @Char(9) ;"\\"+@Char(9) );
temp:=@ReplaceSubstring(temp ; @Char(10) ; "\\"+@Char(10) );
temp:=@ReplaceSubstring(temp ; @Char(12) ; "\\"+@Char(12) );
temp:=@ReplaceSubstring(temp ; @Char(13) ; "\\"+@Char(13) );
temp:=@ReplaceSubstring(temp ; @NewLine ; "");
temp:=@ReplaceSubstring(temp ; @Char(34) ; "\\"+@Char(34) );
json:=json+"\""+_fld[m]+"\":\""+temp+"\",";
m := m + 1);
"{"+json+"\"UID\":\""+@Text(@DocumentUniqueID)+"\"}"
*5)根据2和4再简单,其实和4差不多
_fld:="code":"Quantity":"ITEMNAME";
"{"+ @Implode (@Transform(_fld;"_fn" ; "\"" + _fn + "\":\""
+@Do(
temp:=@Text(@Trim(@GetField(_fn)));
temp:=@ReplaceSubstring(temp ; @Char(92) ; "\\"+@Char(92) );
temp:=@ReplaceSubstring(temp ; @Char(47) ; "\\"+@Char(47) );
temp:=@ReplaceSubstring(temp ; @Char(39) ; "\\"+@Char(39) );
temp:=@ReplaceSubstring(temp ; @Char(8) ;"\\"+@Char(8) );
temp:=@ReplaceSubstring(temp ; @Char(9) ;"\\"+@Char(9) );
temp:=@ReplaceSubstring(temp ; @Char(10) ; "\\"+@Char(10) );
temp:=@ReplaceSubstring(temp ; @Char(12) ; "\\"+@Char(12) );
temp:=@ReplaceSubstring(temp ; @Char(13) ; "\\"+@Char(13) );
temp:=@ReplaceSubstring(temp ; @NewLine ; "");
temp:=@ReplaceSubstring(temp ; @Char(34) ; "\\"+@Char(34) );
temp
)
+ "\"" ) ; ",")+",\"UID\":\""+@Text(@DocumentUniqueID)+"\"}"
*6)根据5更改而来,把一些无意义的有会产生有问题,全转成空格。这样容易理解一些。建议使用
_fld:="code":"Quantity":"ITEMNAME";
"{"+ @Implode (@Transform(_fld;"_fn" ; "\"" + _fn + "\":\""
+@Do(
temp:=@Text(@Trim(@GetField(_fn)));
temp:=@ReplaceSubstring(temp ; @Char(92) ; "\\"+@Char(92) );
temp:=@ReplaceSubstring(temp ; @Char(47) ; "\\"+@Char(47) );
temp:=@ReplaceSubstring(temp ; @Char(39) ; "\\"+@Char(39) );
temp:=@ReplaceSubstring(temp ; @Char(8) ;"" );
temp:=@ReplaceSubstring(temp ; @Char(9) ;"" );
temp:=@ReplaceSubstring(temp ; @Char(10) ;"" );
temp:=@ReplaceSubstring(temp ; @Char(12) ; "" );
temp:=@ReplaceSubstring(temp ; @Char(13) ; "" );
temp:=@ReplaceSubstring(temp ; @NewLine ; "");
temp:=@ReplaceSubstring(temp ; @Char(34) ; "" );
temp
)
+ "\"" ) ; ",")+",\"UID\":\""+@Text(@DocumentUniqueID)+"\"}"