How to create console application which will create JSON based on your input data?
You can use JSON class for serialization and deserialization purpose. HTTP requires standard data which can be used across all processes. You can also assign resultset to different variables or can used for testing purpose.
Description: If you need JSON to test web-api, you can use following lines of code.
I have created one class which is used for mentioning getter and setter properties for request and response.i.e.Classname.
Values assigned to the propoerties in class get serialized or deserialized.
This class takes user's input data convert it in the JSON format and will copy it in the clipboard. You can paste clipboard data in the request body of any web debugging tool for e.g. fiddler and test the functionality.
Model namespace has to added with 'using' at top of the program.
using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Threading.Tasks;
yournamespacename
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Classname clsobj = new Classname();
clsobj.propertyname1 = "Put input data 1";
clsobj.propertyname2 = "Put input data 2";
string strconv = Serializer_And_Deserializer.Conv_For_Serialization
Clipboard.SetText(strconv);
Console.WriteLine("You can paste data from clipboard");
Console.ReadLine();
}
}
}
You can take readymade open source JSON helper classes from google or can create by you own.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
public class Serializer_And_Deserializer
{
public static string Conv_For_Serialization
{
DataContractJsonSerializer obj_for_serializer = new DataContractJsonSerializer(typeof(T));
MemoryStream user_memory_stream = new MemoryStream();
obj_for_serializer.WriteObject(user_memory_stream, userobj);
string strConv = Encoding.UTF8.GetString(user_memory_stream.ToArray());
user_memory_stream.Close();
return strConv;
}
public static T Conv_For_DeSerialization
{
DataContractJsonSerializer obj_for_Deserializer = new DataContractJsonSerializer(typeof(T));
MemoryStream user_memory_stream = new MemoryStream(Encoding.UTF8.GetBytes(strConv));
T resultop = (T)obj_for_Deserializer.ReadObject(user_memory_stream);
return resultop;
}
}