【问题标题】:JsonReaderException when trying to deserialize json string尝试反序列化 json 字符串时出现 JsonReaderException
【发布时间】:2013-12-23 10:56:11
【问题描述】:

我尝试反序列化从 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 信息

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() 

【问题讨论】:

  • 嗨 Stefan,你解决了这个问题吗?
  • 是的,但我很抱歉。不记得怎么了。

标签: c# json sendgrid


【解决方案1】:

遇到同样的问题,看起来响应有空行。

.TrimEnd() 修复了问题,还不错请 'sendgridplus-csharp' 添加相同的修复。

Dim jsonData As String = sr.ReadToEnd().TrimEnd()

如果您要在任何地方序列化您的 EventData,还要修复“SendGridCategoryConverter”:

//// Left as an exercise to the reader :)

serializer.Serialize(writer, value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2013-12-13
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多