我正在尝试将数据发布到json api。 这就是我的代码看起来的样子。它从服务器返回一个错误,那里的人告诉我,我发送的所有内容都是json字符串,我应该发布只有一个参数是JSON stirng的params。即。 contact_attributes应该包含如下所示的字符串:
“{\” 名称\ “:\” 雷\ “\ ”姓\“:\ ”达迪\“,\ ”passport_number \“:\” 00000020 \”,\ “unique_identifier \”:\ “7353280000000020 \”,\ “MSISDN \”: \ “27829042259 \”,\ “bank_account_number \”:空,\“bank_account_br anch_code \ “:空,\” bank_account_branch_name \ “:空,\” bank_detai ls_required \ “:虚假,\” membership_number \ “:空,\” bank_account_b ank_id \ “:空,\” bank_account_account_type_id \ “:空,\” bank_accou nt_initials \ “:空,\” bank_account_surname \ “:空,\” bank_account_p assport_number \ “:空,\” DATE_OF_BIRTH \ “:空,\” debit_day \ “:空,\” CON tract_sale_date \ “:空,\” 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;
}
答案 0 :(得分:1)
尝试使用以下函数处理JSON sring。
private string ConvertToJSON(objType obj)
{
string sJSON = "{";
try {
AppendToJSON(ref sJSON, "obj1Name", obj.obj1);
AppendToJSON(ref sJSON, "obj2Name", obj.obj2);
AppendToJSON(ref sJSON, "obj3Name", obj.obj3);
AppendToJSON(ref sJSON, "obj4Name", obj.obj4);
return sJSON.Substring(0, sJSON.Length - 1) + "}";
} catch (Exception ex) {
return "";
}
}
private void AppendToJSON(ref string byValJSON, string Name, string Value)
{
byValJSON = byValJSON + "\"" + Name + "\":\"" + Value + "\",";
}
将字符串发布到服务器。
using System.Web;
public class AttributesHandler : IHttpHandler
{
public void ProcessRequest(System.Web.HttpContext context)
{
string JSON = null;
string timeOut = null;
timeOut = System.Web.HttpContext.Current.Server.ScriptTimeout;
try {
System.Web.HttpContext.Current.Server.ScriptTimeout = 600;
System.Web.HttpContext.Current.Response.ContentType = "text/javascript";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
JSON = ConvertToJSON(obj);
System.Web.HttpContext.Current.Response.Write(JSON);
System.Web.HttpContext.Current.Server.ScriptTimeout = timeOut;
} catch (Exception ex) {
}
}
}
使用DotNet代码调用:
string Url = "";
Url = "http://xxxxxxxxxxxxxx/AttributesHandler.ashx?cd=xx";
object @out = null;
try {
Net.WebClient webclient = new Net.WebClient();
webclient.UseDefaultCredentials = true;
@out = System.Text.Encoding.ASCII.GetString((webclient.DownloadData(Url)));
} catch (Exception ex) {
//out = ex.InnerException.ToString
}
这里他们只会将响应变为字符串。
使用Javascript Ajax。
$.ajax({
type: "GET", timeout: 60000, cache: false, dataType: "jsonp", url: "http://xxxxxxxxxxxxxx/AttributesHandler.ashx?cd=xx"
success: function (response) {
objAttributes = response.Attributes; //in object formate
if (objCardAttributes != null) {
//can process the objects
}
__doPostBack('upToolkit',);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var sReturn = "Please close the browser and relaunch ";
if (textStatus != null) { sReturn += " Details: (" + textStatus + ")"; }
if (errorThrown != null) { sReturn += " Description: (" + errorThrown.Description + ")"; }
__doPostBack('upToolkit', sReturn);
}
});
答案 1 :(得分:0)
你的json和你的POCO不匹配......没有必要发布多个参数来向服务器发送数据..但是你的json在这种情况下没有翻译成POCO就是问题
“{\”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_accou nt_initials \”:null,\“bank_account_surname \”:null,\“bank_account_p assport_number \”:null, \“date_of_birth \”:null,\“debit_day \”:null,\“con tract_sale_date \”:null,\“contract_start_date \”:null}“
转到www.json2csharp.com看你的json是不可解析的......
确保你的json是对的..
然后使用此站点创建csharp类并反序列化到该特定对象