附:IRIS JSON使用示例

  • Util.JSONUtils
Class Util.JSONUtils Extends (%RegisteredObject, %JSON.Adaptor)
{

/// UsingJSON
/// d ##class(Util.JSONUtils).UsingJSON()
ClassMethod UsingJSON()
{
	/* 创建动态实例 */
	s person = {}
	#; s person = ##class(%DynamicObject).%New()
	s person.Relationships = []
	#; s person.Relationships = ##class(%DynamicArray).%New()
	
	#; s person.IDNum = "342622199304055257"
	d person.%Set("IDNum", "342622199304055257")
	d person.%Set("Name", "wanyh")
	
	s Relationship = {}
	d Relationship.%Set("Relation", "father")
	d Relationship.%Set("Name", "wanxw")
	d person.Relationships.%Push(Relationship)
	
	s Relationship = {}
	d Relationship.%Set("Relation", "mother")
	d Relationship.%Set("Name", "tonggx")
	d person.Relationships.%Push(Relationship)
	zw person
	#; person={"Relationships":[{"Relation":"father","Name":"wanxw"},{"Relation":"mother","Name":"tonggx"}],"IDNum":"342622199304055257","Name":"wanyh"}  ; <DYNAMIC OBJECT>

	/* 动态实例转为JSON字符串 */
	s jsonPerson = person.%ToJSON()
	w jsonPerson,!
	s jsonStreamPerson = ##class(%Stream.GlobalCharacter).%New()
	d person.%ToJSON(jsonStreamPerson)
	#; w jsonStreamPerson.Read(),!
	
	/* JSON字符串转为动态实例 */
	s person = {}.%FromJSON(jsonPerson)
	zw person
	s person = {}.%FromJSON(jsonStreamPerson.ReadLineIntoStream())
	#; zw person
	#; person={"Relationships":[{"Relation":"father","Name":"wanxw"},{"Relation":"mother","Name":"tonggx"}],"IDNum":"342622199304055257","Name":"wanyh"}  ; <DYNAMIC OBJECT>
	
	/* 解析动态实例 */
	#; w "IDNum:"_person.IDNum,!
	w "IDNum:"_person.%Get("IDNum"),!
	w "Name:"_person.%Get("Name"),!
	
	#; s numR = person.Relationships.%Size()
	#; for i = 0 : 1 : numR - 1{
	#; 	 w "Relation:"_person.Relationships.%Get(i).Relation,!	
	#; 	 w "Name:"_person.Relationships.%Get(i).Name,!
	#; }
	
	s itr = person.Relationships.%GetIterator()
	while itr.%GetNext(.key, .val){
		w "Relation:"_val.%Get("Relation"),!
		w "Name:"_val.%Get("Name"),!
	}
	#; IDNum:342622199304055257
	#; Name:wanyh
	#; Relation:father
	#; Name:wanxw
	#; Relation:mother
	#; Name:tonggx
}

/// %JSON.Adaptor
/// d ##class(Util.JSONUtils).JSONAdaptor()
ClassMethod JSONAdaptor()
{
	/* 创建对象 */
	s person = ##class(MyApp.Person).%New()
	s person.IDCard = "342622199304055257"
	s person.Name = "wanyh"
	
	s Relationship = ##class(MyApp.Relationship).%New()
	s Relationship.Relation = "father"
	s Relationship.Name = "wanxw"
	d person.Relationships.Insert(Relationship)
	s Relationship = ##class(MyApp.Relationship).%New()
	s Relationship.Relation = "mother"
	s Relationship.Name = "tonggx"
	d person.Relationships.Insert(Relationship)
	w person,!
	#; 17@MyApp.Person
	
	/* 对象导出为JSON字符串 */
	d person.%JSONExportToString(.jsonPerson)
	w jsonPerson,!
	d person.%JSONExportToStream(.jsonStreamPerson)
	#;w jsonStreamPerson.Read(),!
	#; {"IDNum":"342622199304055257","Name":"wanyh","Relationships":[{"Relation":"father","Name":"wanxw"},{"Relation":"mother","Name":"tonggx"}]}
	
	/* JSON字符串导入对象 */
	s person = ##class(MyApp.Person).%New()
	d person.%JSONImport(jsonPerson)
	w person,!
	#; 28@MyApp.Person
	
	/* 解析JSON对象 */ 
	w "IDNum:"_person.IDCard,!
	w "Name:"_person.Name,!
	
	#; s numR = person.Relationships.Size
	#; for i = 1 : 1 : numR {
	#;	 w "Relation:"_person.Relationships.GetAt(i).Relation,!
	#;	 w "Name:"_person.Relationships.GetAt(i).Name,!
	#; }
	
	while person.Relationships.GetNext(.key){
		w "Relation:"_person.Relationships.GetAt(key).Relation,!
		w "Name:"_person.Relationships.GetAt(key).Name,! 
	}
	
	#; IDNum:342622199304055257
	#; Name:wanyh
	#; Relation:father
	#; Name:wanxw
	#; Relation:mother
	#; Name:tonggx
}

}

  • MyApp.Person
Class MyApp.Person Extends (%Persistent, %JSON.Adaptor) [ SqlTableName = MyAppPerson ]
{

/// Define a unique index for the IDCard property.
Index IDCardKey On IDCard [ Unique ];

/// Person's IDCard number.
Property IDCard As %String(%JSONFIELDNAME = "IDNum", PATTERN = "18N") [ Required ];

/// Name of the person.
Property Name As %String [ Required ];

/// Person's Relationships.
Property Relationships As list Of MyApp.Relationship;

}

  • MyApp.Relationship
Class MyApp.Relationship Extends (%Persistent, %JSON.Adaptor) [ SqlTableName = Relationship ]
{

Property Relation As %String;

Property Name As %String;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wanyh2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值