在方法PostAsync

时间:2018-11-26 11:12:01

标签: c# json.net dotnet-httpclient

尝试发送帖子消息时出现错误

  string productJson = await Task.Run(() => JsonConvert.SerializeObject(product, Formatting.Indented));
  HttpContent content = new StringContent(productJson, Encoding.UTF8, "application/json");
  Uri uri = new Uri(string.Format(@"{0}/api/product/addProduct?token={1}", Configuration.ServerURL, _token.TokenValue));

  var response = await _client.PostAsync(uri, content);

产品型号:

[XmlRoot(ElementName = "Product")]
public class Product
{
    [JsonProperty(PropertyName = "name")]
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "code")]
    [XmlElement(ElementName = "Code")]
    public string Code { get; set; }
    [JsonProperty(PropertyName = "ean")]
    [XmlElement(ElementName = "Ean")]
    public string Ean { get; set; }
    [JsonProperty(PropertyName = "description")]
    [XmlElement(ElementName = "Description")]
    public string Description { get; set; }
    [JsonProperty(PropertyName = "value1")]
    [XmlElement(ElementName = "Value1")]
    public string Value1 { get; set; }
    [JsonProperty(PropertyName = "value2")]
    [XmlElement(ElementName = "Value2")]
    public string Value2 { get; set; }
    [JsonProperty(PropertyName = "productExternalNumber")]
    [XmlElement(ElementName = "ProductExternalNumber")]
    public int? ProductExternalNumber { get; set; }
    [JsonIgnore]
    [XmlElement(ElementName = "ProductExternalType")]
    public int? ProductExternalType { get; set; }
    [JsonProperty(PropertyName = "id")]
    [XmlElement(ElementName = "Id")]
    public int? DefaultStageId { get; set; }
    [JsonProperty(PropertyName = "unitName")]
    [XmlElement(ElementName = "UnitName")]
    public string UnitName { get; set; }
    [JsonProperty(PropertyName = "sourceIntegrationCode")]
    [XmlElement(ElementName = "SourceIntegrationCode")]
    public string SourceIntegrationCode { get; set; }
    [JsonProperty(PropertyName = "externalStorehouseId")]
    [XmlElement(ElementName = "ExternalStorehouseId")]
    public string ExternalStorehouseId { get; set; }
}

序列化后的我的JSON:

{   “ name”:“ Bruynzeel Twin Felt Tip Flamastry 20szt”,   “代码”:“ BS-2020K20B”,   “ ean”:“ 2010000000090”,   “ description”:“”,   “ value1”:“”,   “ value2”:“”,   “ productExternalNumber”:9   “ id”:0,   “ unitName”:“ szt。”,   “ sourceIntegrationCode”:“ XL”,   “ externalStorehouseId”:“ 2” }

我尝试使用Postman发送Post消息,并且有效。

完全错误

  

Newtonsoft.Json.JsonReaderException:解析值:A.路径”,行0,位置0时遇到意外字符。      在Newtonsoft.Json.JsonTextReader.ParseValue()      在Newtonsoft.Json.JsonTextReader.Read()      在Newtonsoft.Json.JsonReader.ReadForType(JsonContract合同,布尔hasConverter)      在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)中      在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)      在Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型,JsonSerializerSettings设置)      在Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,JsonSerializerSettings设置)      在Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value)      在Y.PluginPackingIntegration.Controllers.BaseIntegrationController处。<> c__DisplayClass9_0.b__0()      在System.Threading.Tasks.Task`1.InnerInvoke()      在System.Threading.Tasks.Task.Execute()

1 个答案:

答案 0 :(得分:0)

在这里找到答案: URL

但是我仍然不知道,为什么我在方法PostAsync上遇到反序列化错误。