我想解析此字符串....但由于“message_of_the_day”键,JSON无法识别字符串,因为它包含newLine字符(\ n)。我将如何解析这个JSON字符串,剩下的所有JSON web服务对我来说工作正常。如何解析JSON字符串中的newLine字符
JSON响应:
{"tblMessageOfTheDay":[{"customer_id":"1659","application_id":"1001","message_of_the_day":"Set your Message
GDSFSFDS
SF
ADS
DSFS
F"}]}
代码:
我给了很多尝试解决了这个问题.....但仍然有同样的问题。
-(void)DataCollectedSuccessfully:(NSString *)responseString
{
NSDictionary *results = [responseString JSONValue]; // Main code .. with didn;t work
id result = [responseString JSONValue]; // I tried for it, it that object won;t get recognized, but this trick didn't work.
NSLog(@"%@",result);
NSDictionary *results = [[responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] JSONValue]; // Even tried to remove the newLine characters & extra space.
}
错误:
-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0xa'\" UserInfo=0x6235db0 {NSLocalizedDescription=Unescaped control character '0xa'}",
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: message_of_the_day\" UserInfo=0x6224a50 {NSUnderlyingError=0x6235e00 \"Unescaped control character '0xa'\", NSLocalizedDescription=Object value expected for key: message_of_the_day}",
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x6224a80 {NSUnderlyingError=0x6235d20 \"Object value expected for key: message_of_the_day\", NSLocalizedDescription=Expected value while parsing array}",
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: tblMessageOfTheDay\" UserInfo=0x6224af0 {NSUnderlyingError=0x6224ab0 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: tblMessageOfTheDay}"
)
在有效的JSON中,控制字符(U + 0000到U + 001F,其中包括\ n = U + 000A)**必须**被转义。你的JSON字符串不是有效的JSON,这就是为什么JSON解析器在抱怨它。如有疑问,http://jsonlint.com是验证JSON字符串的绝佳服务。 – 2011-10-15 07:30:49
@Bavarious我已经检查出JSON解析器上的这个字符串...如果我删除多余的消息之间的空间,然后它显示我有效的JSON。但我需要坚持相同的格式... :( –
你可能会能够找到一个更宽容的解析器,但我知道的两个解析器,SBJSON和JSONKit,将拒绝解析无效的JSON。 – 2011-10-15 21:18:33