how to get json format document from sql database..
how to get json format from sql database
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pradeep ShetPosted Aug 28, 2014, 2:46 AM
You can do one thing, get the data from sql datatabase in datatable and the convert that dt object into json object using DataContractJsonSerializer class which is available in System.Runtime.Serialization.Json namespace. I am pasting the code for serializing and de-serializing. Pass the object to this methods which you wish to convert into json.
public static string JsonSerializer
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
public static T JsonDeserialize
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
Hope this helps you to solve your problem.
Plz mark it answered if it helped you.
Pradeep ShetPosted Sep 1, 2014, 2:29 PM
SrujanPosted Sep 1, 2014, 12:56 AM