Click here to Skip to main content
12,951,971 members (64,739 online)
Rate this:
 
Please Sign up or sign in to vote.
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 7-Apr-13 21:17pm
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:

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("}");
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 9 Dec 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100