IOS JSON反序列化失败 - STIG / NSJSONSerializer [英] IOS JSON Deserialization failure - STIG/NSJSONSerializer

查看:245
本文介绍了IOS JSON反序列化失败 - STIG / NSJSONSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化我从IOS中运行的Web服务收到的简单JSON。

I want to deserialize a simple JSON I receive from a web service I run in IOS.

@"[{\"NickName\":\"James Roeiter3\",\"TempId\":\"634783760669935686\",\"LDAP\":\"XUserName15\",\"SecToken\":null},{\"NickName\":\"James Roeiter2\",\"TempId\":\"634783760654574807\",\"LDAP\":\"XUserName16\",\"SecToken\":null}]" 

之前我做过很多次,
我尝试使用NSJsonSerializer进行反序列化还使用STIG:

I have done this many times before , I try to deserialize it both using NSJsonSerializer and also using STIG :

NSArray* array1= (NSArray*)[jsonString JSONValue]; 

NSArray* array2 = (NSArray*)[NSJSONSerialization JSONObjectWithData:jsonString   options: NSJSONReadingMutableContainers error:&error];

现在,问题很奇怪,如果我将JSON从调试器复制粘贴到硬编码中字符串,并尝试解析它,它工作,如果我在运行时收到它,它失败,STIG说它JSON不能以'['开头。
我试过遍历查找null char的所有字符(我不太清楚它是什么)但是复制/粘贴和运行时字符串之间的所有字符看起来都是一样的。
我也尝试格式化为UTF8并使用格式播放 - 也失败了。

NOW , the issue is weird , if I copy paste the JSON from the debugger into an hard-coded string and try to parse it , it works , if I receive it in runtime , it fails , STIG says it JSON cannot start with '[' . I tried going through all the characters looking for null char (which I am not quite sure what it is ) but all the chars looked the same between the copy/paste and the runtime strings. I also tried formatting to UTF8 and play a bit with the formats -- also failed.

知道它可以是什么或者如何解决? ?我真的会在这里找到你的帮助,因为我非常坚持这个假设是一个简单的问题,但在我解决这个问题之前无法取得进展......

Any idea what can it be or how can it be solved ?? I will really appricate your help here as I am pretty stuck with this suppose to be simple issue but cant progress until I solve this ......

编辑:
我收到的确切错误是:

EDIT : The exact error I receive is this :

2012-07-21 23:59:31.376 Whisper.Client.IOS[1058:fe03] Error : Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No string key for value in object around character 1.) UserInfo=0xc9632e0 {NSDebugDescription=No string key for value in object around character 1.}

最重要的是对你们所有人,

Best of regards to you all,

James

推荐答案

好的,问题是转义字符和引号字符\,这就是为什么当它复制并粘贴它作为硬代码时它起作用的原因,因为当硬编码时,编译器只读取引号字符。
如果有人在将来遇到这个问题,这可能会非常烦人:
问题是服务器url编码数据(C#服务器端),IOS url decode因其弱点和apprenlty而闻名不要从字符串中删除\字符。

Ok ,the problem was the escape character and the quote character \", this is why when copying and pasting it as hard code it worked because when hardcoded the compiler reads only the quote " character. This can be very annoying if someone gets into this issue in the future : The problem is that the server url encoded the data (C# server side) , IOS url decode is known for its weakness and apprenlty does not remove the \" character from the string.

这是我当前的IOS解码代码:

This was my current IOS decoding code :

json = [json stringByReplacingOccurrencesOfString:@"+" withString:@" "];
json = [json stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
json = [json stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""];
int lastChar = [json length]-1;
json = [json stringByReplacingCharactersInRange:NSMakeRange(lastChar,1) withString:@""];
json = [json stringByReplacingOccurrencesOfString:@"\\" withString:@""];
return  json;

如果首先从json的开头和结尾删除引号,那么我在chars之前删除所有\字符。

If first remove the quotes from the beginning and end of the json , then I remove all the \ chars before the " chars.

现在这似乎运作顺利,如果你认为我做错了什么请纠正我。

This appears to be working smoothly now , if you think I am doing something wrong please correct me .

希望有一天能帮到某人。

Hope that will help someone someday.

干杯,

詹姆斯

这篇关于IOS JSON反序列化失败 - STIG / NSJSONSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆