-2

您好,我正在解析 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
4

1 回答 1

3

我通过将 /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"];

现在我得到了输出。这个问题是因为服务器端换行符。

于 2012-12-31T08:19:19.157 回答