如何 return json 数据

how to return the json data

我想将字符串转换为 json

这里我想将字符串 data 对象转换为 json

当我使用网络服务页面插入数据时见下文

插入的数据如下所示:

观看window

代码:

    public class WebService1 : System.Web.Services.WebService
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string InsertData(string fname, string mname, string lname, string emailid, string password, string contactno, string hobby, string address, string countrycodenum)
        {
            cn.Open();
            string data = fname + mname + lname + emailid + password + contactno + hobby + address + countrycodenum;

            string insertquery = "insert into tblstudent(fname, mname, lname, emailid, password, contactno,hobby,address,countrycodenum)values(@fname,@mname,@lname,@emailid,@password,@contactno,@hobby,@address,@countrycodenum)";

            SqlCommand cmd = new SqlCommand(insertquery, cn);
            //cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@fname", fname);
            cmd.Parameters.AddWithValue("@mname", mname);
            cmd.Parameters.AddWithValue("@lname", lname);
            cmd.Parameters.AddWithValue("@emailid", emailid);
            cmd.Parameters.AddWithValue("@password", password);
            cmd.Parameters.AddWithValue("@contactno", contactno);
            cmd.Parameters.AddWithValue("@hobby", hobby);
            cmd.Parameters.AddWithValue("@address", address);
            cmd.Parameters.AddWithValue("@countrycodenum", countrycodenum);

            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Console.WriteLine("Insert Successfully");
            }
            else
            {            
                Console.WriteLine("Not Insert Successfully");
            }
            cn.Close();

            return data;
        }
    }
}

我安装了 Newtonsoft 软件包

我想将字符串数据对象转换为 json??

而不是这一行

string data = fname + mname + lname + emailid + password + contactno + hobby + address + countrycodenum;

执行这些两个步骤。

首先使用这行代码创建一个匿名对象

var dataObject = new { fname, mname, lname, emailid, password, contactno, hobby, address, countrycodenum };

在您的示例中,对象将如下所示:

{ fname = raju, mname = makvana, lname = dinesh, emailid = raju@gmail.com, password = 12345, contactno = 1234567890, hobby = cricket, address = surat, countrycodenum = 乌兹别克斯坦 }

其次,序列化dataObjectJSON string

string data = JsonConvert.SerializeObject(dataObject);

别忘了使用 Newtonsoft.Json 包。

在您的示例中,JSON string 将如下所示:

{"fname":"raju","mname":"makvana","lname":"dinesh","emailid":"raju@gmail.com","password":"12345","contactno":"1234567890","hobby":"cricket","address":"surat","countrycodenum":"uzbekistan"}

完整代码如下:

public class WebService1 : System.Web.Services.WebService
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string InsertData(string fname, string mname, string lname, string emailid, string password, string contactno, string hobby, string address, string countrycodenum)
    {
        cn.Open();

        // *** this is old code ***
        // string data = fname + mname + lname + emailid + password + contactno + hobby + address + countrycodenum;

        // *** this is new code ***
        var dataObject = new { fname, mname, lname, emailid, password, contactno, hobby, address, countrycodenum };
        string data = JsonConvert.SerializeObject(dataObject);

        string insertquery = "insert into tblstudent(fname, mname, lname, emailid, password, contactno,hobby,address,countrycodenum)values(@fname,@mname,@lname,@emailid,@password,@contactno,@hobby,@address,@countrycodenum)";

        SqlCommand cmd = new SqlCommand(insertquery, cn);
        //cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@fname", fname);
        cmd.Parameters.AddWithValue("@mname", mname);
        cmd.Parameters.AddWithValue("@lname", lname);
        cmd.Parameters.AddWithValue("@emailid", emailid);
        cmd.Parameters.AddWithValue("@password", password);
        cmd.Parameters.AddWithValue("@contactno", contactno);
        cmd.Parameters.AddWithValue("@hobby", hobby);
        cmd.Parameters.AddWithValue("@address", address);
        cmd.Parameters.AddWithValue("@countrycodenum", countrycodenum);

        int i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            Console.WriteLine("Insert Successfully");
        }
        else
        {            
            Console.WriteLine("Not Insert Successfully");
        }
        cn.Close();

        return data;
    }
}

因此您可以使用 newtonsoft.json 库并定义您的 class 并将其转换为您的 class

      dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(serviceResult);
     foreach (var item in obj1.data)
     {
      //convert obj1 to your class
     }