2016-08-15 47 views
0

我想將我的字符串用戶名和密碼添加到字符串jsonData中,但似乎有錯誤。Json C#將變量添加到json字符串

public async Task<string> authLogin(string username, string password) 
    { 
     var client = new HttpClient(); 
     client.BaseAddress = new Uri("http://172.20.129.193/"); 

     string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}"; 

     var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); 
     HttpResponseMessage response = await client.PostAsync("NYPStudentLifeService/api/student/login",content); 

     string result = await response.Content.ReadAsStringAsync(); 
     return result; 
    } 

回答

0

使用RestSharp進行REST API調用,NewtoSoft將您的對象序列化爲JSON。你的代碼已經過時了。

http://restsharp.org/

http://www.newtonsoft.com/json

順便說......你的錯誤是 「密碼」

的順序在

string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}"; 

應該

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}"; 
0

您使用的方式是錯誤的嘗試這種方法。如上所述,密碼順序也是錯誤的。^

相反的:

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}"; 

你可以試試這個:

string jsonData = @"{'AdminNo':'"+username+"','Password':'"+password+"'}";