Reputation:
I try to deserialize this json string i get from sendgrid. I use sendgridplus repository by hakanL: https://github.com/HakanL/sendgridplus-csharp
[{"email":"[email protected]","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"}]
Here is the method i am using:
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()
Upvotes: 0
Views: 2807
Reputation: 332
got same issue, looks like response had empty line.
.TrimEnd()
fixed issue, not bad to ask 'sendgridplus-csharp' add same fix.
Dim jsonData As String = sr.ReadToEnd().TrimEnd()
also fix 'SendGridCategoryConverter' if you going to serialize your EventData anywhere:
//// Left as an exercise to the reader :)
serializer.Serialize(writer, value);
Upvotes: 1