I have a sample of data which is given below.
{
"ditems": [
{
"type": "ditem",
"name": "webmet.com",
"ditem": 0,
"links": [
"www.abc.com/a",
"www.sempo.org/"
]
},
{
"type": "ditem",
"name": "webmet.com/who-we-are/careers",
"ditem": 2,
"links": [
"http://www.tele12.com/about",
"http://tele12.com/life-at-teletech-en-US/about-teletech/"
]
}
],
"themes": [
{
"type": "theme",
"name": "http://searchm.com/isr/agenda",
"description": "",
"slug": "http://searchm.com/isr/agenda-2"
},
{
"type": "theme",
"name": "http://www.sempo.org/",
"description": "",
"slug": "http://www.sempo.org/-2"
}
]
}
Here is my code
var InternalURLList = dtInternalURL.AsEnumerable().Select(c => c.Field<string>("URL")).Distinct().ToList();
StringBuilder sb = new StringBuilder();
StringBuilder ThemeSb = new StringBuilder();
sb.Append("{\"ditems\":[");
if (InternalURLList.Count > 0)
{
for (int i = 0; i < InternalURLList.Count; i++)
{
var ExternalDomainList = GetExternalDomain(Domain_ID, InternalURLList[i]).AsEnumerable().Select(c => c.Field<string>("ExternalDomain")).Distinct().ToList();
sb.Append("{\"type\":\"ditem\",");
sb.Append("\"name\":");
sb.Append("\"" + InternalURLList[i] + "\",");
sb.Append("\"ditem\":" + i + ",");
sb.Append("\"links\":[");
if (ExternalDomainList.Count > 0)
{
for (int j = 0; j < ExternalDomainList.Count; j++)
{
sb.Append("\"" + ExternalDomainList[j] + "\"");
sb.Append((j != ExternalDomainList.Count - 1) ? "," : "]") ;
}
}
else
{
sb.Append("]");
}
sb.Append("}");
sb.Append((i != InternalURLList.Count - 1)?",":"]");
}
}
else
{
sb.Append("]");
sb.Append(",");
}
DataTable dtDistinctDomain = GetDistinctExtDomain(Domain_ID);
var ExternalDomain = dtDistinctDomain.AsEnumerable().Select(c => c.Field<string>("ExternalDomain")).Distinct().ToList();
ThemeSb.Append(",\"themes\":[");
for (int i = 0; i < ExternalDomain.Count; i++)
{
// For theme
ThemeSb.Append("{\"type\":\"theme\",");
ThemeSb.Append("\"name\":\"" + ExternalDomain[i] + "\",");
ThemeSb.Append("\"description\":\"\",");
ThemeSb.Append("\"slug\":\"" + ExternalDomain[i] + "-2" + "\"}");
ThemeSb.Append((i != ExternalDomain.Count - 1)?",":"]");
}
ThemeSb.Append("}");
string ConceptMapTheme = ThemeSb.ToString() ;
string ConceptMapJSON = sb.ToString()+ThemeSb.ToString();
return ConceptMapJSON;
I am trying to make json in C# using StringBuilder
. The for
loop is taking too much time to generate the json. The table contains more than 2,00,000 records. How to create json in a fastest way using C#?