Pages

Sunday, 19 January 2014

Convert list of list to json data

public static string ListToJson<T>(List<List<T>> GroupList, string Object, string[] arrDtls, string StatusCode, string Message)
    {
        int Count = 0;
        StringBuilder sb = new StringBuilder();
        try
        {
            if (GroupList != null && StatusCode == "200" && Message == "Success")
            {
                sb.Append("{\"Status\":\"" + StatusCode + "\",");
                sb.Append("\"Message\":\"" + Message + "\",");
                sb.Append("\"" + Object + "\":");
                sb.Append("[{");
                int LstCount = GroupList.Count;
                foreach (var TempLst in GroupList)
                {
                    sb.Append("\"" + arrDtls[Count] + "\":");
                    sb.Append(JsonConvert.SerializeObject(TempLst));
                    Count++;
                    sb.Append("},{");
                    if (Count == LstCount)
                    {
                        int index = EsHelper.EsGetValidString(sb).Length;
                        sb.Remove(index - 2, 2);
                    }
                }
                sb.Append("]}");
            }
            else
            {
                sb.Append("{\"Status\":\"" + StatusCode + "\",");
                sb.Append("\"Message\":\"" + Message + " \"}");
            }
        }
        catch (Exception ex)
        {
            EsHelper.EsADLogger("Logistics.aspx ListToJson<T>()", ex.Message.ToString());
            StringBuilder ExSb = new StringBuilder();
            ExSb.Append("{\"Status\":\"" + StatusCode + "\",");
            ExSb.Append("\"Message\":\" Service Failed During Json Conversion  \"}");
            return EsHelper.EsGetValidString(ExSb);
        }
        return EsHelper.EsGetValidString(sb);
    }






[WebMethod]
        public static string CollectionRefPrdDtls(string ReferenceNo)
        {
            string ProductCodeObj = string.Empty;
            string StatusCode = "200";
            string StatusMsg = "Success";
            string ObjectDtls = "RefName|RefPrdDtls";
            string[] arrObjectDtls = new string[2];
            arrObjectDtls = ObjectDtls.Split('|');
            try
            {
                using (CollectionRefDtls CollRefName = new CollectionRefDtls())
                {
                    List<List<CollectionRefDtls>> CombinedList = new List<List<CollectionRefDtls>>();
                    List<CollectionRefDtls> LstCollRefName = new List<CollectionRefDtls>();
                    List<CollectionRefDtls> LstCollRefDtls = new List<CollectionRefDtls>();
                    using (workflow.Components.Inventory DAL = new workflow.Components.Inventory())
                    {
                        SqlDataReader Dr;
                        SqlParameter[] param = new SqlParameter[2];
                        param[0] = new SqlParameter("@Flag", "B");
                        param[1] = new SqlParameter("@CollectionRefId", ReferenceNo);
                        using (Dr = DAL.ReadData(Usp_CollectionRefOpr, param))
                        {
                            if (Dr.HasRows)
                            {
                                while (Dr.Read())
                                {
                                    LstCollRefName.Add(new CollectionRefDtls()
                                    {
                                        CollRefName = EsHelper.EsGetValidString(Dr["CollectionReferenceName"])
                                    });
                                }

                            }
                            CombinedList.Add(LstCollRefName);
                            Dr.NextResult();
                            if (Dr.HasRows)
                            {
                                while (Dr.Read())
                                {
                                    LstCollRefDtls.Add(new CollectionRefDtls()
                                    {
                                        CollRefPrd2ndCtgName = EsHelper.EsGetValidString(Dr["Prd2ndCtgName"]).ToLower().Replace(" ", "-"),
                                        CollRefPrd3rdCtgName = EsHelper.EsGetValidString(Dr["Prd3rdCtgName"]).ToLower().Replace(" ", "-"),
                                        CollRefPrdName = EsHelper.EsGetValidString(Dr["PrdName"]).ToLower().Replace(" ", "-"),
                                        CollRefPrdCode = EsHelper.EsGetValidString(Dr["PrdCode"]).ToLower().Replace(" ", "-")
                                    });
                                }
                                CombinedList.Add(LstCollRefDtls);
                            }
                        }
                    }
                    if (LstCollRefName.Count <= 0 && LstCollRefDtls.Count <= 0)
                    {
                        StatusCode = "204"; StatusMsg = "No Records found";
                    }
                    ProductCodeObj = EsHelper.ListToJson<CollectionRefDtls>(CombinedList, "CollRefDtls", arrObjectDtls, StatusCode, StatusMsg);
                }
            }
            catch (Exception ex)
            {
                EsHelper.EsADLogger("EstoreCollectionRef.aspx CollectionRefPrdDtls()", EsHelper.EsGetValidString(ex.Message));
            }
            return ProductCodeObj;
        }

No comments:

Post a Comment