-1

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#?

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
  • 6
    Take a look at [Json.NET](http://www.newtonsoft.com/json) – Boxiom Sep 10 '15 at 09:08
  • 2
    Why bother manually creating when you can use NewtonsoftJSON using either web api or an mvc web application. like so : `Public JsonResult Index(){return Json(JsonRequestBehaviour.AllowGet,True);}` – TinMan7757 Sep 10 '15 at 09:09
  • 2
    Also take a look in [json2csharp](http://json2csharp.com/) – Rubens Farias Sep 10 '15 at 09:10
  • you have https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.110).aspx – BRAHIM Kamel Sep 10 '15 at 09:10
  • 3
    @Rubens - That functionality is built in to Visual Studio; you don't need to use a website. Go to the top menu in VS Edit>Paste Special>Paste JSON As Classes. – Ulric Sep 10 '15 at 09:14
  • possible duplicate of [Turn C# object into a JSON string in .NET 4](http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4) – Artiom Sep 10 '15 at 09:28
  • Simple google 'C# to json' will give you plenty of results. – Artiom Sep 10 '15 at 09:29
  • You are asking how to create a JSON or Serialize the json object? – Chandrasekar Kesavan Sep 10 '15 at 09:46
  • To serialize or create the json object you have to use Newtonsoft.Json. Best way to create a json is serialize the class with property to Json object. – Chandrasekar Kesavan Sep 10 '15 at 09:49
  • I have updated my code. `for` loop is taking too much time to scan all the node. How should I modify to make it faster? – Musakkhir Sayyed Sep 10 '15 at 09:55

1 Answers1

5

You can add the Newtonsoft.Json nuget package in your project. Then you just need to call the serializer :

var jsonString = JsonConvert.SerializeObject(objectToSerialize);
PMerlet
  • 2,568
  • 4
  • 23
  • 39