How to get json format document from sql database..
how to get json format from sql database
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pradeep ShetRank #595 · 4K reputationPosted Aug 27, 2014, 10:46 PM EDT
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.
Srujan8 reputationPosted Aug 31, 2014, 8:56 PM EDT
Pradeep ShetRank #595 · 4K reputationPosted Sep 1, 2014, 10:29 AM EDT