Click here to Skip to main content
15,877,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hello,
Bellow is My Web Service written in C#.
In this, I got a return value and a table from database.
I want to merge it into single JSON Object/Array. How can I do it..?

con.Open();
        SqlDataAdapter da = new SqlDataAdapter("UpdateUserInfo2 ", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.Add("@UserAutoId", SqlDbType.VarChar).Value = UserAutoId;
        da.SelectCommand.Parameters.Add("@userId", SqlDbType.VarChar).Value = userId;
        da.SelectCommand.Parameters.Add("@firstName", SqlDbType.VarChar).Value = fname;
        da.SelectCommand.Parameters.Add("@lastName", SqlDbType.VarChar).Value = lname;
        da.SelectCommand.Parameters.Add("@emailadr", SqlDbType.VarChar).Value = email;
        da.SelectCommand.Parameters.Add("@hieght", SqlDbType.VarChar).Value = hieght;
        da.SelectCommand.Parameters.Add("@retValue", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.ReturnValue;
        da.Fill(tbl);

        int retval = (int)da.SelectCommand.Parameters["@retValue"].Value;
        if (retval == 1)
        {
            string result = "1";

//Here I want to merge table Object(tbl) and return value (result)

            json = new JavaScriptSerializer().Serialize(result);
        }
        else if (retval == 2)
        {
            string result = "2";
            json = new JavaScriptSerializer().Serialize(result);
        }
        con.Close();
        return "json";
    }


What I have tried:

I trying to update single record on database and in response to query fetch, I want to get updated table and return value(or any object).
Posted
Updated 22-Oct-18 23:50pm
Comments
Ayodeji Oluwatomiwo 6-Jan-17 11:10am    
Why is "json" in quotes? it will return it as a literal (text) not as a json string.
Ayodeji Oluwatomiwo 6-Jan-17 11:26am    
You can create a class for your table columns

public class UserInfo
    {
        public string UserAutoId { get; set; }
        public string userId { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string emailadr { get; set; }
        public string hieght { get; set; }
    }


then select the updated row. You can then serialize your class object.

con.Open();
SqlCommand cmd = new SqlCommand(selectQuery, con);
SqlDataReader reader = cmd.ExecuteReader();
UserInfo userInfo = new UserInfo();
while(reader.Read())
{
info.UserAutoId = reader["UserInfoAutoId"].ToString();
.
.
.

}

//now you can serialize the object
string userJson = ...

From return Table From SP

then

Java
JSONArray obj = new JSONArray();
    try {
         for(int i = 0; i <table.rows.count; i++) {
            // 1st object
            JSONObject list1 = new JSONObject();
            list1.put("val1",i+1);
            list1.put("val2",i+2);
            list1.put("val3",i+3);
            obj.put(list1);
         }
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    Toast.makeText(MainActivity.this, ""+obj, Toast.LENGTH_LONG).show();
 
Share this answer
 
v2
{ "emp": [
{
"employeeId": 52374,
"employeeName": "Mayuresh Chaudhary",
"employeePhone": 7888119073,
"project": "ZENLABS_IIOT",
"manager": "Vikram Samdare",
"status": "inactive",
"assets": [
{
"assetsNo": 30003,
"description": "HP Pavilion - Laptop",
"type": "LAPTOP",
"assetsStatus": "success"
}
],
"manangerPhone": 8888801154
},
{
"employeeId": 42177,
"employeeName": "Nilesh Parekh",
"employeePhone": 9764696566,
"project": "ZENLABS_IIOT",
"manager": "Vikram Samdare",
"manangerPhone": 8888801154,
"status": "active",
"assets": [
{
"assetsNo": 30001,
"description": "HP Pavilion - Laptop",
"type": "LAPTOP",
"assetsStatus": "danger"
},
{
"assetsNo": 30005,
"description": "HP Pavilion - Laptop",
"type": "LAPTOP",
"assetsStatus": "success"
}
]
}
],
"message": "Asset Mismatch"
}
 
Share this answer
 
Comments
Richard Deeming 23-Oct-18 8:48am    
How is this unformatted JSON blob supposed to be a "solution" to the question?

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