ios中的Json值失败[关闭]

Posted

技术标签:

【中文标题】ios中的Json值失败[关闭]【英文标题】:Json value failed in ios [closed] 【发布时间】:2012-12-31 06:47:43 【问题描述】:

您好,我正在解析 JSON 格式的 URL,出现如下错误

JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0xa'\" UserInfo=0x1d224220 NSLocalizedDescription=Unescaped control character '0xa'",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: Location\" UserInfo=0x1d2242d0 NSUnderlyingError=0x1d224260 \"Unescaped control character '0xa'\", NSLocalizedDescription=Object value expected for key: Location",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x1d224310 NSUnderlyingError=0x1d2242f0 \"Object value expected for key: Location\", NSLocalizedDescription=Expected value while parsing array",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: GetIssues\" UserInfo=0x1d224350 NSUnderlyingError=0x1d224330 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: GetIssues"

但如果我在浏览器中点击相同的 URL,它会完美运行。请指导我哪里出错了。

我的 json 输出是

"GetIssues": [ "PhotoUrl" : "http://city311.smartletmanager.com/city311/uploads/Sideways_SWS_122_154.jpg", "IssueName" : "Sidewalk Construction Permit", "Comments" : "N/A", "Location" : "Rajiv Gandhi Salai Tamil Nadu India", "Status" : "Not Fixed", "issueSubmittedDate" : "2012-12-28 12:40:20" , "PhotoUrl" : "http://city311.smartletmanager.com/city311/uploads/Environment_ENV_123_287.jpg", "IssueName" : "Dust Complaint", "Comments" : "N/A", "Location" : "Rajiv Gandhi Salai Tamil Nadu India", "Status" : "Not Fixed", "issueSubmittedDate" : "2012-12-28 12:52:35" , "PhotoUrl" : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_124_342.jpg", "IssueName" : "Resources for passengers with disabilities and taxi drivers", "Comments" : "Tug", "Location" : "Ggj", "Status" : "Fixed", "issueSubmittedDate" : "2012-12-28 14:14:56" ]

如果我尝试在获取 json 值之前打印字符串。我得到了 json 格式的输出:

"GetIssues":
            [ "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Sideways_SWS_122_154.jpg",
                "IssueName"     : "Sidewalk Construction Permit",
                "Comments"      : "N/A",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-28 12:40:20"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Environment_ENV_123_287.jpg",
                "IssueName"     : "Dust Complaint",
                "Comments"      : "N/A",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-28 12:52:35"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_124_342.jpg",
                "IssueName"     : "Resources for passengers with disabilities and taxi drivers",
                "Comments"      : "Tug",
                "Location"          : "Ggj",
                "Status"            : "Fixed",
                "issueSubmittedDate"    : "2012-12-28 14:14:56"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_125_620.jpg",
                "IssueName"     : "Resources for passengers with disabilities and taxi drivers",
                "Comments"      : "Ryffhdugdjstiin",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:17:18"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Sideways_SWS_126_736.jpg",
                "IssueName"     : "Sidewalk Construction Permit",
                "Comments"      : "Drrtg",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:18:26"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_127_84.jpg",
                "IssueName"     : "Blocked Sidewalk or Street Complaint",
                "Comments"      : "Erttyyyy",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:19:04"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_128_642.jpg",
                "IssueName"     : "Sidewalk Repair Protest",
                "Comments"      : "Qwerty",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:19:35"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_129_359.jpg",
                "IssueName"     : "Resources for passengers with disabilities and taxi drivers",
                "Comments"      : "Werttyy",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:20:18"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_130_914.jpg",
                "IssueName"     : "Snow or Ice on Roadway/Sidewalks",
                "Comments"      : "Rtyygguhh",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:20:50"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_131_983.jpg",
                "IssueName"     : "Pedestrian Signal with Walk and Don’t Walk Displaying.",
                "Comments"      : "Rt",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:21:23"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_132_11.jpg",
                "IssueName"     : "Dirty yard or alley complaints",
                "Comments"      : "Truth",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:21:54"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_133_33.jpg",
                "IssueName"     : "Improper Disposal",
                "Comments"      : "Ttyyu",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:22:23"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_134_40.jpg",
                "IssueName"     : "Snow or Ice on Roadway/Sidewalks",
                "Comments"      : "Ytterbium",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:22:56"
           , "PhotoUrl"       : "http://city311.smartletmanager.com/city311/uploads/Transport_TRP_135_689.jpg",
                "IssueName"     : "Illegal Parking",
                "Comments"      : "N/A",
                "Location"          : "Rajiv Gandhi Salai
Tamil Nadu
India",
                "Status"            : "Not Fixed",
                "issueSubmittedDate"    : "2012-12-31 00:31:17"
           ]

但是给予之后

NSArray *issueDetailsAry = [json JSONValue];
//objectForKey:@"GetIssues"];
     NSLog(@"issueDetailsAry %@",issueDetailsAry);

我得到了输出

-JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0xa'\" UserInfo=0x8356b20 NSLocalizedDescription=Unescaped control character '0xa'",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: Location\" UserInfo=0x8357850 NSUnderlyingError=0x8356b70 \"Unescaped control character '0xa'\", NSLocalizedDescription=Object value expected for key: Location",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x83578a0 NSUnderlyingError=0x8356a90 \"Object value expected for key: Location\", NSLocalizedDescription=Expected value while parsing array",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: GetIssues\" UserInfo=0x8357930 NSUnderlyingError=0x83578f0 \"Expected value while parsing array\", NSLo

【问题讨论】:

在某些地方搜索此问题的解决方案时,他们提到删除了所有 \r 和 viola 一切都像魅力一样!但我不知道我必须删除什么。请帮助 e.提前致谢 【参考方案1】:

我通过将 /n 替换为 json 字符串中的空字符串来解决此问题

NSString *json = [[NSString alloc]init];

json = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
json = [json stringByReplacingOccurrencesOfString:@"\n" withString:@""];

// NSLog(@"json %@",json); NSArray *issueDetailsAry = [[json JSONValue]objectForKey:@"GetIssues"];

现在我得到了输出。这个问题是由于服务器端换行造成的。

【讨论】:

以上是关于ios中的Json值失败[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

iOS中的json模式显示[关闭]

如何从 iOS 中的文件中解析 JSON? [关闭]

IOS 推送通知中的问题 - 身份验证失败,因为远程方已关闭传输流

iOS (APN) - 身份验证失败,因为远程方已关闭 C# 中的传输流

将json对象值变成javascript中的键和值[关闭]

从Objective C中的json对象获取值? [关闭]