Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: JSON
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 22:17pm
Comments
Anurag Sinha V at 8-Apr-13 3:36am
   
So, where are you stuck??
Prasad Khandekar at 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 at 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
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 9 Dec 2014
Copyright © CodeProject, 1999-2014
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