我尝试反序列化我从sendgrid获得的这个json字符串。 我使用hakanL的sendgridplus存储库:https://github.com/HakanL/sendgridplus-csharp
[{"email":"ogsasd@gmail.com","Identification":"934224","timestamp":1387734767,"ip":"83.223.255.137","useragent":"Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B511","category":["ReservationConfirmation"," Online"," Salonkey-86432150"," AppointmentId-132347"," SalonId-223"],"event":"open"}]
以下是我使用的方法:
public static List<EventData> GetEvents(string json)
{
// RWM: Deal with v1 and v2 batched Event data.
if (!json.StartsWith("["))
{
json = string.Format("[{0}]", json.Replace("}" + Environment.NewLine + "{", "},{"));
}
// RWM: Hack to deal with SendGrid not being able to get their shit together and send well-formed JSON.
if (!json.EndsWith("]"))
{
json += "]";
}
_logger.Info("Json returned: " + JsonConvert.DeserializeObject<List<EventData>>(json));
return JsonConvert.DeserializeObject<List<EventData>>(json);
}
2013-12-23 10:49:29.0665 INFO
Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: ]. Path '', line 2, position 1.
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at itsperfect.ResponseHandler.Mvc.Utils.GetEventData.GetEvents(String json)
at itsperfect.ResponseHandler.Mvc.Utils.GetEventData.GetEvents(Stream inputStream)
at itsperfect.ResponseHandler.Mvc.Controllers.EmailController.CatchMailEvent()
答案 0 :(得分:1)
得到同样的问题,看起来响应有空行。
.TrimEnd()
修复了问题,不错的问题&#39; sendgridplus-csharp&#39;添加相同的修复程序。
Dim jsonData As String = sr.ReadToEnd().TrimEnd()
还修复了SendGridCategoryConverter&#39;如果你要在任何地方序列化你的EventData:
//// Left as an exercise to the reader :)
serializer.Serialize(writer, value);