Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have created json string using string builder and now i want to convert it to json using json.net without the use of classes.I have given example below.

Thanks in advance.


SqlCommand cmd = new SqlCommand("SELECT tcjc_ID, tcjc_JobCode, tcjc_Name, tcjc_JobDesc1 + tcjc_JobDesc2 + tcjc_JobDesc3, tco_Company_Name, tsi_Site_Name FROM tbl_CreateJobCosting LEFT JOIN tbl_Companies ON tcjc_CompanyId = tco_CompanyId LEFT JOIN tbl_Sites ON tcjc_SiteId = tsi_SiteID WHERE tcjc_ID = " + jobId, con);
sb.Append("{ \"jobId\":\"");
sb.Append(jobId);
sb.Append("\",");
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
sb.Append("\"jobcode\":\"");
sb.Append(reader.GetInt32(1));
sb.Append("\", \"name\":\"");
sb.Append(reader.GetString(2));
sb.Append("\", \"desc\":\"");
sb.Append(reader.GetString(3));
sb.Append("\", \"company\":\"");
sb.Append(reader.GetString(4));
sb.Append("\", \"site\":\"");
sb.Append(reader.GetString(5));
}
Posted
Comments
Anurag Sinha V 8-Apr-13 3:36am    
So, where are you stuck??
Prasad Khandekar 8-Apr-13 15:41pm    
Hello Neeraj,

i want to convert it to json using json.net without the use of classes, is like saying Unless I learn to swim I won't get into the water. Json.Net is a library which allows you to work with JSON data structure and it also provides de/serialization services for converting object into JSON format and vice versa.

Any way without using the classes you have created the JSON string. So where is the problem?
NeerajRathi 9-Apr-13 0:53am    
Hi Prasad
Thanks for suggestion.I have solve the problem using Json.Net.

Thanks
Neeraj

1 solution

Yes, I know its too late, but it would be helpful whenever next one come to find his solution. My solution is convert your data in json string format like:

C#
Dictionary<string,> myValues = new Dictionary<string,>();
            myValues.Add("one", "1");
            myValues.Add("two", "2");
            myValues.Add("three", "3");

            StringBuilder sb = new StringBuilder();
            sb.Append("{");

            bool first = true;

            foreach (string k in myValues.Keys)
            {
                if (!first)
                {
                    sb.Append(",");
                }
                sb.AppendFormat("\"{0}\":\"{1}\"", k, myValues[k]);
                first = false;
            }

            sb.Append("}");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900