Public classTriple { PublicTriple (string_s,string_p,string_o) { This. _s =_s; This. _p =_p; This. _o =_o; } Public string_s {Set;Get; } Public string_p {Set;Get; } Public string_o {Set;Get; } }
function
Static voidTripletransferjson (string_s,refJobject obj) { if(List = =NULL|| List. Count = =0)return; List<Triple> sublist = list. Where (A = a._s = =_s). ToList (); Obj. ADD ("PrimaryKey", _s); //Get Property Countdictionary<string,int> dt =Getpropertycount (sublist); foreach(varIteminchsublist) { if(Item._o.startswith ("\"") && dt[item._p] = =1) obj. ADD (item._p, item._o); if(Item._o.startswith ("\"") && Dt[item._p] >1) { if(obj. ContainsKey (Item._p)) ((Jarray) obj[item._p]). ADD (Item._o); Elseobj. ADD (Item._p,NewJarray (item._o)); } sublist= list. Where (A = a._s = =item._o). ToList (); Jobject jobj=NewJobject (); Tripletransferjson (Item._o,refjobj); if(Dt[item._p] = =1&&!item._o.startswith ("\"") ) obj. ADD (item._p, jobj); if(Dt[item._p] >1&&!item._o.startswith ("\"")) { if(obj. ContainsKey (Item._p)) ((Jarray) obj[item._p]). ADD (Jobj); Elseobj. ADD (Item._p,NewJarray (jobj)); } } } Staticdictionary<string,int> Getpropertycount (list<triple>list) { if(List = =NULL|| List. Count = =0)return NULL; Dictionary<string,int> dt =Newdictionary<string,int>(); varPC = fromPinchList group p by p._p to GSelect New{property = G.key, Count =g.count ()}; foreach(varIteminchpc) {dt. ADD (Item.property, Item.count); } returnDT; }
Test data
List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.name","\ "Wangshuang")); List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.type","\ "People.person")); List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.gender","\ "Female")); List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.name","\ "Sky")); List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.image","HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEFD")); List. ADD (NewTriple ("Http://knowledge.microsoft.com/sfesfesfefs","Type.object.image","HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEF3")); List. ADD (NewTriple ("HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEFD","Type.object.type","\ "Common.image")); List. ADD (NewTriple ("HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEF3","Type.object.type","\ "Common.image")); List. ADD (NewTriple ("HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEFD","Type.image.uri","\ "Http://www.baidu.com")); List. ADD (NewTriple ("HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEF3","Type.image.uri","\ "Http://www.baidu.com")); Jobject obj=NewJobject (); Tripletransferjson ("Http://knowledge.microsoft.com/sfesfesfefs",refobj); stringJSON = obj. ToString ();
Test result
{ "PrimaryKey":"Http://knowledge.microsoft.com/sfesfesfefs", "Type.object.name": [ "\ "Wangshuang", "\ "Sky" ], "Type.object.type":"\ "People.person", "Type.object.gender":"\ "Female", "Type.object.image": [ { "PrimaryKey":"HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEFD", "Type.object.type":"\ "Common.image", "Type.image.uri":"\ "Http://www.baidu.com" }, { "PrimaryKey":"HTTP://KNOWLEDGE.MICROSOFT.COM/SFESFESFEF3", "Type.object.type":"\ "Common.image", "Type.image.uri":"\ "Http://www.baidu.com" } ]}
Triple Transfer JSON