xcode IOS JSON 反序列化失败 - STIG/NSJSONSerializer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11594617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:58:05  来源:igfitidea点击:

IOS JSON Deserialization failure - STIG/NSJSONSerializer

iosxcodejson

提问by James Roeiter

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

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

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

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

我以前做过很多次,我尝试使用 NSJsonSerializer 和 STIG 反序列化它:

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

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

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.

现在,这个问题很奇怪,如果我将调试器中的 JSON 复制粘贴到硬编码字符串中并尝试解析它,它会起作用,如果我在运行时收到它,它会失败,STIG 说它 JSON 不能以 '[ 开头' . 我尝试遍历所有字符以查找空字符(我不太确定它是什么),但复制/粘贴和运行时字符串之间的所有字符看起来都相同。我还尝试格式化为 UTF8 并尝试使用这些格式——也失败了。

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

詹姆士

回答by James Roeiter

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.

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

This was my current IOS decoding code :

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

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;

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

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

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

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

Hope that will help someone someday.

希望有一天会帮助某人。

Cheers ,

干杯,

James

詹姆士