Create Json with StringBuilder in C#

Create Json with StringBuilder in C#

JSON

A JSON (JavaScript Object Notation) is a lightweight data-interchange format string. Easy for humans to read and write. It is easy for machines to parse and generate outcome. Easy to transport data bidirectional. Can be compressed and encrypted upto any level.

StringBuilder

The String object is immutable. Every time you create a string variable using System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly.

The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop. Let’s build an example where we will create a json using StringBuilder class which can be serialized in proper json format using Javascript serializer.

Code example

private string JsonWithStringBuilder()
{
	//Create string builder object
	StringBuilder jsonStringBuilder =new StringBuilder();
	//json base Open braces
	jsonStringBuilder.Append("{");
	//forming an array start
	jsonStringBuilder.Append("\"array\":");
	jsonStringBuilder.Append("[");
	jsonStringBuilder.Append("1,2,3");
	jsonStringBuilder.Append("]");
	//array ends
	jsonStringBuilder.Append(",");
	//creating boolean value
	jsonStringBuilder.Append("\"boolean\": true");
	jsonStringBuilder.Append(",");
	//setting standard key value
	jsonStringBuilder.Append("\"color\": blue");
	jsonStringBuilder.Append(",");
	//setting null value
	jsonStringBuilder.Append("\"null\": null");
	jsonStringBuilder.Append(",");
	//setting number value
	jsonStringBuilder.Append("\"number\": 123456");
	jsonStringBuilder.Append(",");
	//creating object
	jsonStringBuilder.Append("\"object\":");
	jsonStringBuilder.Append("{");
	jsonStringBuilder.Append("\"a\": aValue");
	jsonStringBuilder.Append(",");
	jsonStringBuilder.Append("\"c\": cValue");
	jsonStringBuilder.Append("}");
	jsonStringBuilder.Append(",");
	//creating simple string
	jsonStringBuilder.Append("\"string\": How are you?");
	//json base closing braces
	jsonStringBuilder.Append("}");

	//Create javaScript Serialier class object
	//using System.Web.Script.Serialization
	JavaScriptSerializer jsObjectSerializer = new JavaScriptSerializer();
	jsObjectSerializer.MaxJsonLength = Int32.MaxValue;
	//Finally serialize json into a string
	return jsObjectSerializer.Serialize(jsonStringBuilder.ToString());
}

In above code we have used JavaScript serialize method which will convert string to a valid json, the expected output would look like as below, to enable serializer you need to extend reference for System.Web.Extensions assembly in your project:

Please leave your valuable comments!!!

Leave a Reply

Your email address will not be published. Required fields are marked *