Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过WebRequest进行json.net反序列化时的随机失败_C#_Ios_Json.net - Fatal编程技术网

C# 通过WebRequest进行json.net反序列化时的随机失败

C# 通过WebRequest进行json.net反序列化时的随机失败,c#,ios,json.net,C#,Ios,Json.net,我使用Json.NET反序列化iOS客户机上API服务器发送的Json文件(使用Monotouch)。我有一个非常奇怪的问题,因为有时反序列化会因随机异常而失败。我以前没有遇到任何问题,我认为这是从json文件变大开始的。当它发生时,我只是要求应用程序再试一次,经过随机次数(并随机出现不同的异常)后,反序列化最终会毫无理由地工作 以下是我得到的不同例外: 2013-02-12 19:14:58.307 client_ios[2176:4303] Failed to deserialize:

我使用Json.NET反序列化iOS客户机上API服务器发送的Json文件(使用Monotouch)。我有一个非常奇怪的问题,因为有时反序列化会因随机异常而失败。我以前没有遇到任何问题,我认为这是从json文件变大开始的。当它发生时,我只是要求应用程序再试一次,经过随机次数(并随机出现不同的异常)后,反序列化最终会毫无理由地工作

以下是我得到的不同例外:

  2013-02-12 19:14:58.307 client_ios[2176:4303] Failed to deserialize: Unexpected end of content while loading JObject.  
        at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0 
        at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in <filename unknown>:0 
        at shared.api.NotificationConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) [0x00000] in /Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23 
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0 
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference, Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in <filename unknown>:0 

Failed to deserialize: Unterminated string. Expected delimiter: ". Line 2530, position 69.  at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer (Char quote) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonTextReader.ParseString (Char quote) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonTextReader.ParseValue (Char currentChar) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonTextReader.ReadInternal () [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.JsonTextReader.Read () [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Linq.JContainer.ReadContentFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in <filename unknown>:0 
  at shared.api.NotificationConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) [0x00000] in /Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference,     Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in <filename unknown>:0 
2013-02-12 19:14:58.307客户端ios[2176:4303]无法反序列化:加载JObject时内容意外结束。
位于Newtonsoft.Json.Linq.JContainer.ReadTokenFrom(Newtonsoft.Json.JsonReader r)[0x00000]中:0
在:0中的Newtonsoft.Json.Linq.JObject.Load(Newtonsoft.Json.JsonReader reader)[0x00000]处
在/Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23中的shared.api.NotificationConverter.ReadJson(Newtonsoft.Json.JsonReader reader,System.Type objectType,System.Object existingValue,Newtonsoft.Json.JsonSerializer serializer)[0x00000]
位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.createValueNoProperty(Newtonsoft.Json.JsonReader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract)[0x00000]in:0
位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection包装列表,Newtonsoft.Json.JsonReader,System.String引用,Newtonsoft.Json.Serialization.JsonArrayContract契约)[0x00000]in:0
未能反序列化:未终止的字符串。应为分隔符:“.2530行,位置69。位于Newtonsoft.Json.JsonTextReader.readStringToBuffer(字符引号)[0x00000]中:0”
0中的Newtonsoft.Json.JsonTextReader.ParseString(字符引号)[0x00000]处
0中的Newtonsoft.Json.JsonTextReader.ParseValue(Char currentChar)[0x00000]处
位于:0中的Newtonsoft.Json.JsonTextReader.ReadInternal()[0x00000]
位于:0中的Newtonsoft.Json.JsonTextReader.Read()[0x00000]
位于Newtonsoft.Json.Linq.JContainer.ReadContentFrom(Newtonsoft.Json.JsonReader r)[0x00000]中:0
位于Newtonsoft.Json.Linq.JContainer.ReadTokenFrom(Newtonsoft.Json.JsonReader r)[0x00000]中:0
在:0中的Newtonsoft.Json.Linq.JObject.Load(Newtonsoft.Json.JsonReader reader)[0x00000]处
在/Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23中的shared.api.NotificationConverter.ReadJson(Newtonsoft.Json.JsonReader reader,System.Type objectType,System.Object existingValue,Newtonsoft.Json.JsonSerializer serializer)[0x00000]
位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.createValueNoProperty(Newtonsoft.Json.JsonReader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract)[0x00000]in:0
位于Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection包装列表,Newtonsoft.Json.JsonReader,System.String引用,Newtonsoft.Json.Serialization.JsonArrayContract契约)[0x00000]in:0
以下是我用于反序列化的几行代码:

using (WebResponse webResponse = requestWeb.GetResponse()) {
    using (Stream responseStream = webResponse.GetResponseStream()) {
        using (StreamReader streamReader = new StreamReader(responseStream)) {
            using (JsonReader jsonReader = new JsonTextReader(streamReader)) {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Converters.Add(creator);
                rsp = serializer.Deserialize<SimpleServerResponse>(jsonReader);
            }
        }
     }
}
使用(WebResponse WebResponse=requestWeb.GetResponse()){
使用(Stream responseStream=webResponse.GetResponseStream()){
使用(StreamReader StreamReader=新StreamReader(responseStream)){
使用(JsonReader JsonReader=newjsontextreader(streamReader)){
JsonSerializer serializer=新的JsonSerializer();
serializer.Converters.Add(创建者);
rsp=serializer.Deserialize(jsonReader);
}
}
}
}
它在JObject.Load(读卡器)处崩溃:

公共类NotificationConverter:JsonConverter{
公共覆盖布尔CanConvert(类型objectType){
返回类型(ANotification).IsAssignableFrom(objectType);
}
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序){
JObject=JObject.Load(读卡器);
对象目标=null;
JToken token=jobject[“type_id”];
if(令牌!=null){
int id=token.ToObject();
开关(id){
case ANotification.STORY_邀请:
目标=新通知();
打破
案例说明。故事内容添加:
目标=新通知();
打破
case ANotification.STORY_LIKE:
目标=新通知();
打破
案例ANotification.PICTURE_评论:
目标=新通知();
打破
case ANotification.PICTURE_喜欢:
目标=新通知();
打破
案例说明。用户请遵循:
目标=新通知();
打破
违约:
打破
}
填充(jobject.CreateReader(),目标);
}
回报目标;
}
}
关于我的JsonConverter的一些解释:服务器在JSON文件上发送的对象可能会根据“type_id”值的不同而变化。JsonConverter尝试读取type_id并根据它返回正确的对象

目前我的解决方法是读取字符串中的整个流,然后将其传递给json反序列化程序,但在iOS上它肯定会慢一些(占用更多CPU)

我不知道为什么它有时有效,有时无效。

检查您的javascript(或生成JSON的任何东西)是否使用有效密钥传递未定义的值。服务器(在Java中,使用Jackson)每次尝试向同一请求发送相同的JSON,因此这一点毫无疑问。当JSON.net反序列化程序读取字符串而不是
public class NotificationConverter : JsonConverter {

    public override bool CanConvert(Type objectType) {
        return typeof(ANotification).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        JObject jobject = JObject.Load(reader);

        object target = null;

        JToken token = jobject["type_id"];

        if (token != null) {
            int id = token.ToObject<int>();

            switch (id) {
                case ANotification.STORY_INVITE:
                    target = new Notification<Moment>();
                    break;
                case ANotification.STORY_CONTENT_ADDED:
                    target = new Notification<Moment>();
                    break;
                case ANotification.STORY_LIKE:
                    target = new Notification<Moment>();
                    break;
                case ANotification.PICTURE_COMMENTED:
                    target = new Notification<PictureMomentPair>();
                    break;
                case ANotification.PICTURE_LIKED:
                    target = new Notification<PictureMomentPair>();
                    break;
                case ANotification.USER_FOLLOW:
                    target = new Notification<User>();
                    break;
                default:
                    break;
            }

            serializer.Populate(jobject.CreateReader(), target);
        }

        return target;
    }
}