Iphone 如何使用字符串分隔的组件解析json
我是解析JSON的新手,我正在尝试一项简单的任务,从forecast weather JSON文件中检索URL 在这里,我解析json并记录数据每个组件的内容:Iphone 如何使用字符串分隔的组件解析json,iphone,ios,json,Iphone,Ios,Json,我是解析JSON的新手,我正在尝试一项简单的任务,从forecast weather JSON文件中检索URL 在这里,我解析json并记录数据每个组件的内容: NSError *myError = nil; NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError]; NSArray *data
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *data = [res objectForKey:@"data"];
NSLog(@"data=%@",data);
NSArray *results = [data valueForKey:@"weather"];
NSLog(@"weather=%@",results);
NSArray *results1 = [results valueForKey:@"tempMaxC"];
NSLog(@"tempMaxC=%@",results1);
NSArray *results2 = [results1 valueForKey:@"weatherIconUrl"];
NSLog(@"weatherIconUrl=%@",results2);
问题是,当我得到WeatherIconUrl时,它带有这种格式
"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
如果没有引号,我无法获取url本身,我尝试使用nsrange和componentsSeparatedByString,但它总是给我以下错误:
[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance
来自服务器的JSON:
{
"data": {
"current_condition": [
{
"cloudcover": "0",
"humidity": "73",
"observation_time": "12:19 PM",
"precipMM": "0.0",
"pressure": "1021",
"temp_C": "23",
"temp_F": "73",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "NW",
"winddirDegree": "320",
"windspeedKmph": "17",
"windspeedMiles": "11"
}
],
"request": [
{
"query": "Fanzeres, Portugal",
"type": "City"
}
],
"weather": [
{
"date": "2012-09-12",
"precipMM": "0.0",
"tempMaxC": "28",
"tempMaxF": "83",
"tempMinC": "17",
"tempMinF": "63",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "NW",
"winddirDegree": "312",
"winddirection": "NW",
"windspeedKmph": "16",
"windspeedMiles": "10"
},
{
"date": "2012-09-13",
"precipMM": "0.0",
"tempMaxC": "33",
"tempMaxF": "91",
"tempMinC": "17",
"tempMinF": "63",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "N",
"winddirDegree": "8",
"winddirection": "N",
"windspeedKmph": "10",
"windspeedMiles": "6"
}
]
}
}
对不起,我的英语不好,如果我做错了,请纠正我,提前谢谢如果您的URL周围确实有引号,请尝试以下方法:
NSString *someURLString = [results2 objectAtIndex:0];
NSString *quotesRemoved = [someURLString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
如果URL周围确实有引号,请尝试以下操作:
NSString *someURLString = [results2 objectAtIndex:0];
NSString *quotesRemoved = [someURLString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
从@“weatherIconUrl”获取数组时,请使用objectForKey而不是valueForKey,然后将字符串放入NSString中,例如
NSString *weatherIconUrlString = [results2 objectAtIndex:0]
要检查此url是否有效,请使用NSURLConnection的CanHandlerRequest方法,例如
NSURL *url = [NSURL URLWithString:weatherIconUrlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
BOOL canGo = [NSURLConnection canHandleRequest:request];
从@“weatherIconUrl”获取数组时,请使用objectForKey而不是valueForKey,然后将字符串放入NSString中,例如
NSString *weatherIconUrlString = [results2 objectAtIndex:0]
要检查此url是否有效,请使用NSURLConnection的CanHandlerRequest方法,例如
NSURL *url = [NSURL URLWithString:weatherIconUrlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
BOOL canGo = [NSURLConnection canHandleRequest:request];
通过服务器的输出,可以更容易地读取json格式 下面的代码现在根据需要获取天气图标url。它假定json已作为名为jsonData的NSData对象下载,并且不检查数据引用的日期
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves
error:&error];
NSArray *data = [jsonDict valueForKey:@"data"];
NSArray *weather = [data valueForKey:@"weather"];
NSArray *weatherIcon = [[weather objectAtIndex:0] valueForKey:@"weatherIconUrl"];
NSString *url = [[weatherIcon objectAtIndex:0] valueForKey:@"value"];
生成的url在NSURLRequest中使用,并显示在webview中
通过服务器的输出提供了一种更易于阅读的json格式 下面的代码现在根据需要获取天气图标url。它假定json已作为名为jsonData的NSData对象下载,并且不检查数据引用的日期
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves
error:&error];
NSArray *data = [jsonDict valueForKey:@"data"];
NSArray *weather = [data valueForKey:@"weather"];
NSArray *weatherIcon = [[weather objectAtIndex:0] valueForKey:@"weatherIconUrl"];
NSString *url = [[weatherIcon objectAtIndex:0] valueForKey:@"value"];
生成的url在NSURLRequest中使用,并显示在webview中
显示调用ComponentSeparatedByStringc的代码可以发布从服务器检索到的JSON结果吗?您也不能在
NSArray上componentsSeparatedByString:
将其作为NSString
的方法进行对象。是的,请使用JSONParser,不要在NSArray对象上调用componentsSeparatedByString,显然它会崩溃,由于NSArray没有任何名为componentsseparatedbystringprepost的方法,因此请从服务器发布响应。另外,您是如何测试它以确定URL是否包含引号的?这是服务器响应:显示调用ComponentSeparatedByStringCan的代码您可以发布从服务器检索到的JSON结果吗?您也不能在NSArray上componentsSeparatedByString:
将其作为NSString
的方法进行对象。是的,请使用JSONParser,不要在NSArray对象上调用componentsSeparatedByString,显然它会崩溃,由于NSArray没有任何名为componentsseparatedbystringprepost的方法,因此请从服务器发布响应。另外,您是如何测试它以确定URL是否包含引号的?这是服务器响应:这是来自服务器的响应:尝试了您的方法并获得-[\uu NSArrayI stringByTrimmingCharactersInSet:]:无法识别的选择器发送到实例。谢谢你的帮助。你知道你的json是如何构造的吗?然后得到你想要修剪的绳子。我假设字符串在该数组中,但它们在其他地方。我真的不明白你是如何设置结果数组的,这让我很难确切地告诉你该怎么做。但是我在json中没有看到额外的引号。您能解释一下如何验证这些引号的存在吗?这是服务器的响应:尝试了您的方法并获得-[\uu NSArrayI stringByTrimmingCharactersInSet:]:无法识别的选择器发送到实例。谢谢你的帮助。你知道你的json是如何构造的吗?然后得到你想要修剪的绳子。我假设字符串在该数组中,但它们在其他地方。我真的不明白你是如何设置结果数组的,这让我很难确切地告诉你该怎么做。但是我在json中没有看到额外的引号。您能解释一下您是如何验证这些引号的存在的吗?使用[res objectForKey:@“weatherIconUrl]”获取了一个空值;感谢到目前为止的帮助。使用[res objectForKey:@“weatherIconUrl”]获取空值;谢谢你迄今为止的帮助。