Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hello,
I want to get convert multiple data object into JSON string using C#.
Like.. 1-I have a data table which I get from database using store procedure
and 2-I have a string("userType:1") like this.
I want to return both of data object (table and string) in my json String. How can I do it, Any Idea...?

here is my Code.
[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string getLogin(string id, string password)
    {
        string json = "";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        try
        {
            con.Open();

            DataTable tblUserInfo = new DataTable();
            SqlDataAdapter da;
            da = new SqlDataAdapter("SP1_login ", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@userId", SqlDbType.VarChar).Value = id;
            da.SelectCommand.Parameters.Add("@password", SqlDbType.VarChar).Value = password;
            da.SelectCommand.Parameters.Add("@retValue", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.ReturnValue;
            da.Fill(tblUserInfo);
            if (tblUserInfo.Rows.Count != 0)
            {   
                // Convert DataTable to JSON using JSON.Net DLL.         
                json = DataTableToJSONWithJSONNet(tblUserInfo);
            }
            else
            {
                string result = "2";
                json = new JavaScriptSerializer().Serialize(result);              
            }
        }
        catch (Exception ex)
        {
           
        }
        finally
        {
            con.Close();
        }
        return json;
    }

In above code JSON string contains Only data table

What I have tried:

I am trying to get data at user login.
I want return JSON such that I get login User Information that contain in table and another object is UserType like local user or Admin or any other.

At client side I want to get two data object in JSON string One is Data table and another one is UserType
Posted
Updated 9-Jan-17 22:47pm

If you are using JSON.NET you can call Merge on two JSON Objects.

JObject obj1 = JObject.Parse(@"{
  'field1': 'value1',
  'field2': 'value2'
}");
JObject obj2 = JObject.Parse(@"{
  'field3': 'somevalue'
}");

obj1.Merge(obj2, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });

string json = obj1.ToString();
 
Share this answer
 
Comments
Vikas Hire 10-Jan-17 8:17am    
I think it would be work, but I get error. I already get convert the table tblUserInfo into json so this json not converting to ison object (please see my code above)
 
Share this answer
 
Comments
Vikas Hire 10-Jan-17 4:02am    
Resp, John Simmons Your sol.n is not suitable for my problem, I already converted my table using third option that you suggest to me.

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