Beispiel #1
0
        /// <summary>
        ///     Converts a Json string to ExpandoObject method applicable for multi hierarchy objects i.e
        ///     having zero or many parent child relationships
        /// </summary>
        /// <param name="json">string formated as Json</param>
        /// <returns>System.Dynamic.ExpandoObject Json object<see cref="ExpandoObject" />ExpandoObject</returns>
        public static dynamic JsonToExpanderObject(this string json)
        {
            var converter = new ExpandoObjectConverter();

            return(JsonConvert.DeserializeObject <ExpandoObject>(json, converter));
        }
Beispiel #2
0
        public ActionResult ParsTask(int IdTaskB24, string Domen, string Hesh, string NameTbl)
        {
            List <DataAbonent> badabonent = new List <DataAbonent>();  // Список пустых абонентов
            var    converter = new ExpandoObjectConverter();
            string content;

            using (xNet.HttpRequest request = new xNet.HttpRequest())
            {
                content = request.Get("https://" + Domen + "/rest/" + Hesh + "/tasks.task.get?id=" + IdTaskB24).ToString();
            }
            dynamic       obj     = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
            List <string> lstabon = new List <string>();

            foreach (var item in obj.result.task.ufCrmTask)
            {
                lstabon.Add(item);
            }
            // List<object[]> lsttel = new List<object[]>();
            //try
            //{
            //    lsttel = sql.SelectUserTel(NameTbl);
            //}catch
            //{

            //}
            sql.CreateTable(NameTbl);

            foreach (var item in lstabon)
            {
                DataAbonent abonent  = new DataAbonent();
                string[]    dataabon = item.Split('_');
                if (dataabon[0] == "C")
                {
                    using (xNet.HttpRequest request = new xNet.HttpRequest())
                    {
                        content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.contact.get?id=" + dataabon[1]).ToString();
                    }
                    dynamic obj2 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                    content          = System.Text.RegularExpressions.Regex.Unescape(content);
                    abonent.datajson = content;
                    abonent.Type     = "Контакт";
                    abonent.IdEnt    = dataabon[1];
                    try
                    {
                        abonent.telef = obj2.result.PHONE[0].VALUE;
                    }
                    catch { continue; }
                }
                else if (dataabon[0] == "CO")
                {
                    using (xNet.HttpRequest request = new xNet.HttpRequest())
                    {
                        content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.company.get?id=" + dataabon[1]).ToString();
                    }
                    dynamic obj3 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                    content          = System.Text.RegularExpressions.Regex.Unescape(content);
                    abonent.datajson = content;
                    abonent.Type     = "Компания";
                    abonent.IdEnt    = dataabon[1];
                    try
                    {
                        abonent.telef = obj3.result.PHONE[0].VALUE;
                    }
                    catch { continue; }
                }
                else if (dataabon[0] == "L")
                {
                    using (xNet.HttpRequest request = new xNet.HttpRequest())
                    {
                        content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.lead.get?id=" + dataabon[1]).ToString();
                    }
                    dynamic obj4 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                    content          = System.Text.RegularExpressions.Regex.Unescape(content);
                    abonent.datajson = content;
                    abonent.Type     = "Лид";
                    abonent.IdEnt    = dataabon[1];
                    try
                    {
                        abonent.telef = obj4.result.PHONE[0].VALUE;
                    }
                    catch { continue; }
                }
                else if (dataabon[0] == "D")
                {
                    using (xNet.HttpRequest request = new xNet.HttpRequest())
                    {
                        content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.deal.get?id=" + dataabon[1]).ToString();
                    }
                    dynamic obj5 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                    System.Threading.Thread.Sleep(400);
                    if (obj5.result.COMPANY_ID != "0")
                    {
                        using (xNet.HttpRequest request = new xNet.HttpRequest())
                        {
                            content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.company.get?id=" + obj5.result.COMPANY_ID).ToString();
                        }
                        dynamic obj3 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                        content          = System.Text.RegularExpressions.Regex.Unescape(content);
                        abonent.datajson = content;
                        abonent.Type     = "Сделка";
                        abonent.IdEnt    = dataabon[1];
                        try
                        {
                            abonent.telef = obj3.result.PHONE[0].VALUE;
                        }
                        catch { continue; }
                    }
                    else
                    {
                        if (obj5.result.CONTACT_ID == null)
                        {
                            continue;
                        }
                        using (xNet.HttpRequest request = new xNet.HttpRequest())
                        {
                            content = request.Get("https://" + Domen + "/rest/" + Hesh + "/crm.contact.get?id=" + obj5.result.CONTACT_ID).ToString();
                        }
                        dynamic obj2 = JsonConvert.DeserializeObject <ExpandoObject>(content, converter);
                        content          = System.Text.RegularExpressions.Regex.Unescape(content);
                        abonent.datajson = content;
                        abonent.Type     = "Сделка";
                        abonent.IdEnt    = dataabon[1];
                        try
                        {
                            abonent.telef = obj2.result.PHONE[0].VALUE;
                        }
                        catch { continue; }
                    }
                }

                string strtel = new string(abonent.telef.Where(t => char.IsDigit(t)).ToArray());
                strtel = func.FilterNumber(strtel);
                // lsttel = sql.SelectUserTel(NameTbl);
                if (string.IsNullOrEmpty(strtel))
                {
                    abonent.Title = "Пустой телефон или не верный формат";
                    badabonent.Add(abonent);
                    continue;
                }
                if (!sql.HaveRowsTel(NameTbl, strtel))
                {
                    sql.ImportDataTel(NameTbl, strtel, abonent.datajson, IdTaskB24);
                    count++;
                }
                else
                {
                    abonent.Title = "Дубль";
                    badabonent.Add(abonent);
                }
                System.Threading.Thread.Sleep(400);
            }
            ViewBag.Count = count;
            return(View(badabonent));

            return(Content("<div style=\"text-align: center; padding: 20px; font-size: 22px; \"><div>Импорт завершен! Всего: " + count + " записей.</div><div><a href=\"/\">Назад</a></div></div>"));
        }
        public IEnumerable <IAnalysisSet> CreateInstance(string json)
        {
            ///TODO: separar la conversion de json con la generacion del objeto
            List <IAnalysisSet> result = new List <IAnalysisSet>();

            ExpandoObjectConverter converter = new ExpandoObjectConverter();
            dynamic jsonObject;

            try
            {
                jsonObject = JsonConvert.DeserializeObject <ExpandoObject>(json, converter);
            }
            catch (ArgumentNullException e)
            {
                result = null;
                throw new InvalidJSONException(e.Message, e.InnerException);
            }
            catch (JsonSerializationException e)
            {
                result = null;
                throw new InvalidJSONException(e.Message, e.InnerException);
            }

            if (jsonObject == null || !DynamicExtensions.HasProperty(jsonObject, "configuration") || !DynamicExtensions.HasProperty(jsonObject.configuration, "Count"))
            {
                throw new InvalidJSONException();
            }

            foreach (var config in jsonObject.configuration)
            {
                if (!DynamicExtensions.HasProperty(config, "type"))
                {
                    throw new InvalidJSONException();
                }

                switch (config.type)
                {
                case AnalysisSet.ClassType:
                    List <IAnalysis> analysis = new List <IAnalysis>();

                    if (!DynamicExtensions.HasProperty(config, "name") || !DynamicExtensions.HasProperty(config, "analyses") || !DynamicExtensions.HasProperty(config.analyses, "Count"))
                    {
                        throw new InvalidJSONException();
                    }

                    foreach (var analisysConfig in config.analyses)
                    {
                        analysis.Add(this.AnalysisTypesFactory.CreateInstance(analisysConfig));
                    }

                    IAnalysisSet analysisSet = this.AnalysisSetFactory.CreateInstance(config.name, analysis);
                    result.Add(analysisSet);
                    break;

                default:
                    throw new InvalidJSONException();
                }
            }

            return(result);
        }