我从c#调用PHP函数,如下所示
using (var client = new WebClient())
{
string URL = "http://site.or/services/LoadMemberData.php";
NameValueCollection formData = new NameValueCollection();
formData["id"] = "123";
byte[] responseBytes = client .UploadValues(URL, "POST", formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
Console.WriteLine(responsefromserver);
}
一旦完成,responsefromserver
包含一些错误消息,如果出现错误或成员的详细信息,以防成功
如下
{"result":{"success":true,"message":"","errorcode":0},"response":{"id":"123","full_name":"tom Vin","mobile_no":"02343434","phone_no":null,"country_code":"123312","country_of_residence":"","email":"ff@gmail.com","passport_no":"hedf"}}
我如何将结果分成变量?
答案 0 :(得分:1)
您可以通过json2csharp工具生成模型,如下所示:
public class Result
{
public bool success { get; set; }
public string message { get; set; }
public int errorcode { get; set; }
}
public class Response
{
public string id { get; set; }
public string full_name { get; set; }
public string mobile_no { get; set; }
public object phone_no { get; set; }
public string country_code { get; set; }
public string country_of_residence { get; set; }
public string email { get; set; }
public string passport_no { get; set; }
}
public class RootObject
{
public Result result { get; set; }
public Response response { get; set; }
}
然后使用Newtonsoft.Json
库反序列化您的输入:
var input =
"{\"result\":{\"success\":true,\"message\":\"\",\"errorcode\":0},\"response\":{\"id\":\"123\",\"full_name\":\"tom Vin\",\"mobile_no\":\"02343434\",\"phone_no\":null,\"country_code\":\"123312\",\"country_of_residence\":\"\",\"email\":\"ff@gmail.com\",\"passport_no\":\"hedf\"}}";
var result = JsonConvert.DeserializeObject<RootObject>(input);
编辑:基于@Sir Rufo评论:您可以使用jsonutils.com生成模型。该站点允许您生成如下数据注释:
[DataContract]
public class Response
{
[DataMember(Name="full_name")]
public string FullName { get; set; }
因此您可以为字段设置方便的C#命名