ClassUtil.JSONUtilsExtends(%RegisteredObject,%JSON.Adaptor){/// UsingJSON/// d ##class(Util.JSONUtils).UsingJSON()ClassMethodUsingJSON(){/* 创建动态实例 */
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()ClassMethodJSONAdaptor(){/* 创建对象 */
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,!
#;}whileperson.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
ClassMyApp.PersonExtends(%Persistent,%JSON.Adaptor)[SqlTableName=MyAppPerson]{/// Define a unique index for the IDCard property.IndexIDCardKeyOnIDCard[Unique];/// Person's IDCard number.PropertyIDCardAs%String(%JSONFIELDNAME ="IDNum", PATTERN ="18N")[Required];/// Name of the person.PropertyNameAs%String[Required];/// Person's Relationships.PropertyRelationshipsAs list OfMyApp.Relationship;}