Triple transfer json

本文介绍了一种将基于Triple对象的数据结构转换为JSON格式的方法,适用于处理知识图谱中的实体属性及关系数据,并通过示例展示了如何实现多层级的数据转换。
  public class Triple
    {
        public Triple(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 void TripleTransferJson(string _S, ref JObject obj)
        {
             if (list == null || list.Count == 0) return;
            List<Triple> sublist = list.Where(a => a._S == _S).ToList();
            obj.Add("PrimaryKey", _S);
            //get property Count
            Dictionary<string, int> dt = GetPropertyCount(sublist);
            foreach (var item in sublist)
            {
                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);
                    else
                        obj.Add(item._P, new JArray(item._O));
                }

                sublist = list.Where(a => a._S == item._O).ToList();
                JObject jObj = new JObject();
                TripleTransferJson(item._O, ref jObj);
                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);
                    else
                        obj.Add(item._P, new JArray(jObj));

                }
            }




        }
        static Dictionary<string, int> GetPropertyCount(List<Triple> list)
        {
            if (list == null || list.Count == 0) return null;
            Dictionary<string, int> dt = new Dictionary<string, int>();
            var pc = from p in list
                     group p by p._P into g
                     select new { property = g.Key, count = g.Count() };
            foreach (var item in pc)
            {
                dt.Add(item.property, item.count);
            }
            return dt;
        }

test data

  list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.name", "\"Wangshuang"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.type", "\"people.person"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.gender", "\"female"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.name", "\"sky"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.image", "http://knowledge.microsoft.com/sfesfesfefd"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefs", "type.object.image", "http://knowledge.microsoft.com/sfesfesfef3"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefd", "type.object.type", "\"common.image"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfef3", "type.object.type", "\"common.image"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfefd", "type.image.uri", "\"http://www.baidu.com"));
            list.Add(new Triple("http://knowledge.microsoft.com/sfesfesfef3", "type.image.uri", "\"http://www.baidu.com"));

            
            JObject obj = new JObject();
            TripleTransferJson("http://knowledge.microsoft.com/sfesfesfefs", ref obj);
            string json = 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"
    }
  ]
}

 

转载于:https://www.cnblogs.com/skywss27/p/8978132.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值