我正在尝试将数据发布到 json api。这就是我的代码的样子。它从服务器返回一个错误,那里的人告诉我,我将所有内容作为 json 字符串发送,并且我应该发布只有一个参数是 JSON 搅拌的参数。IE。contact_attributes 应该包含一个字符串,如下所示:
"{\"name\":\"Leigh\",\"surname\":\"Duddy\",\"passport_number\":\" 00000020\",\"unique_identifier\":\"7353280000000020\", \"msisdn\": \"27829042259\",\"bank_account_number\":null,\"bank_account_br anch_code\":null,\"bank_account_branch_name\":null,\"bank_detai ls_required\":false,\"membership_number\ ":null,\"bank_account_b ank_id\":null,\"bank_account_account_type_id\":null,\"bank_account_initials\":null,\"bank_account_surname\":null,\"bank_account_p assport_number\":null,\"date_of_birth \":null,\"debit_day\":null,\"contract_sale_date\":null,\"contract_start_date\":null}"
我完全糊涂了,有谁知道我该怎么做。
public class JsonSaleData
{
[DataMember]
public string contact_attributes { get; set; }
[DataMember]
public string campaign_uuid = "9d119cce-25ea-46bc-b7bc-cba7e8323e91";
[DataMember]
public string user_credentials = "OCPdNbeltviij8C1RLcf";
[DataMember]
public string license_id = "1";
}
private string PostSaleToClient(string url, JsonSaleData _SaleData)
{
string strResult = "";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json; charset=utf-8";
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonSaleData));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, _SaleData);
ms.Position = 0;
String json = Encoding.UTF8.GetString(ms.ToArray());
StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
System.Net.HttpWebResponse httpWebResp = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpWebResp.GetResponseStream()))
{
strResult = streamReader.ReadToEnd();
}
return strResult;
}
catch (Exception ex)
{
string e = ex.Message;
}
return strResult;
}