刚刚接触iOS,正在做自己的第一个iOS App,现在遇到这样一个问题。
我从WCF上获得的Json格式如下:
{
Fri = (
"\U8fc7\U7a0b\U63a7\U5236\U7cfb\U7edf\U4e0e\U4eea\U8868;\U5468\U4e94\U7b2c1,2\U8282{\U7b2c7-16\U5468};\U9ad8\U5b8f\U4f1f;XX-226",
"MATLAB\U5e94\U7528;\U5468\U4e94\U7b2c3,4\U8282{\U7b2c9-16\U5468};\U8d75\U5ef6\U4e1c;XX-128;;\U8ba1\U7b97\U673a\U8fc7\U7a0b\U63a7\U5236\U6280\U672f;\U5468\U4e94\U7b2c3,4\U8282{\U7b2c1-8\U5468};\U9ad8\U5b8f\U4f1f;XX-226",
"\U7f51\U7edc\U5316\U6d4b\U63a7\U7cfb\U7edf;\U5468\U4e94\U7b2c5,6\U8282{\U7b2c1-12\U5468};\U80e1\U745e/\U9648\U4eae;XX-228",
"\U5de5\U63a7\U7ec4\U6001\U8f6f\U4ef6\U53ca\U5e94\U7528;\U5468\U4e94\U7b2c7,8\U8282{\U7b2c1-8\U5468};\U9a6c\U666f\U5bcc;XX-228;;\U81ea\U52a8\U63a7\U5236\U539f\U7406\Uff08\U63d0\U9ad8\Uff09;\U5468\U4e94\U7b2c7,8\U8282{\U7b2c9-16\U5468};\U5218\U6d77\U8273;XX-229",
"<null>",
"<null>"
);
Mon = (
"\U5d4c\U5165\U5f0f\U4eea\U8868\U8bbe\U8ba1\U57fa\U7840;\U5468\U4e00\U7b2c1,2\U8282{\U7b2c9-16\U5468};\U5218\U519b/\U77f3\U5f81\U9526;XX-229",
"\U7535\U5b50\U6d4b\U91cf\U6280\U672f;\U5468\U4e00\U7b2c3,4\U8282{\U7b2c1-16\U5468};\U8d75\U4e9a\U5a01/\U9ad8\U5b8f\U4f1f;XX-130",
"\U4fe1\U53f7\U4e0e\U7cfb\U7edf\Uff08\U63d0\U9ad8\Uff09;\U5468\U4e00\U7b2c5,6\U8282{\U7b2c1-8\U5468};\U534e\U5b87\U5b81;XX-130;;\U53ef\U7f16\U7a0b\U63a7\U5236\U5668;\U5468\U4e00\U7b2c5,6\U8282{\U7b2c9-16\U5468};\U66f2\U4e07\U6625;XX-336",
"\U667a\U80fd\U4eea\U5668\U4eea\U8868;\U5468\U4e00\U7b2c7,8\U8282{\U7b2c1-12\U5468};\U66f2\U4e07\U6625;XX-336",
"<null>",
"<null>"
);}而我现在制作的代码只能将Fri跟Mon放入TableView,代码如下:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"test";
NSString *testUrl = @"testurl";
NSURL *url = [NSURL URLWithString:testUrl];
//第二步,通过URL创建网络请求
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
NSData *JSONData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *jsonResult = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
self.str = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
self.data = jsonResult; NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"DictionaryJson is %@",dJSON);
NSMutableArray *_names=[[NSMutableArray alloc]init];
for (id item in jsonResult)
{
NSArray *array=[jsonResult valueForKey:item];
[_names addObject:[NSString stringWithFormat:@"%@ %@", item,joinedComponents]];;
self.names = _names;
NSLog(@"SELF.NAME IS:%@", self.names);
}
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.names count];
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 7.0 ];
cell.textLabel.font = myFont;
cell.textLabel.text = self.names[indexPath.row];
return cell;
}
我现在想要的效果是,将Json中用“,”分开的部分放在单独的TableViewCell里,如果这种不行,放在同一个TableViewCell的不同行里也是可以接受的。由于我没有办法对返回的Json进行修改,所以只能这么来了...求各位大神解答
我从WCF上获得的Json格式如下:
{
Fri = (
"\U8fc7\U7a0b\U63a7\U5236\U7cfb\U7edf\U4e0e\U4eea\U8868;\U5468\U4e94\U7b2c1,2\U8282{\U7b2c7-16\U5468};\U9ad8\U5b8f\U4f1f;XX-226",
"MATLAB\U5e94\U7528;\U5468\U4e94\U7b2c3,4\U8282{\U7b2c9-16\U5468};\U8d75\U5ef6\U4e1c;XX-128;;\U8ba1\U7b97\U673a\U8fc7\U7a0b\U63a7\U5236\U6280\U672f;\U5468\U4e94\U7b2c3,4\U8282{\U7b2c1-8\U5468};\U9ad8\U5b8f\U4f1f;XX-226",
"\U7f51\U7edc\U5316\U6d4b\U63a7\U7cfb\U7edf;\U5468\U4e94\U7b2c5,6\U8282{\U7b2c1-12\U5468};\U80e1\U745e/\U9648\U4eae;XX-228",
"\U5de5\U63a7\U7ec4\U6001\U8f6f\U4ef6\U53ca\U5e94\U7528;\U5468\U4e94\U7b2c7,8\U8282{\U7b2c1-8\U5468};\U9a6c\U666f\U5bcc;XX-228;;\U81ea\U52a8\U63a7\U5236\U539f\U7406\Uff08\U63d0\U9ad8\Uff09;\U5468\U4e94\U7b2c7,8\U8282{\U7b2c9-16\U5468};\U5218\U6d77\U8273;XX-229",
"<null>",
"<null>"
);
Mon = (
"\U5d4c\U5165\U5f0f\U4eea\U8868\U8bbe\U8ba1\U57fa\U7840;\U5468\U4e00\U7b2c1,2\U8282{\U7b2c9-16\U5468};\U5218\U519b/\U77f3\U5f81\U9526;XX-229",
"\U7535\U5b50\U6d4b\U91cf\U6280\U672f;\U5468\U4e00\U7b2c3,4\U8282{\U7b2c1-16\U5468};\U8d75\U4e9a\U5a01/\U9ad8\U5b8f\U4f1f;XX-130",
"\U4fe1\U53f7\U4e0e\U7cfb\U7edf\Uff08\U63d0\U9ad8\Uff09;\U5468\U4e00\U7b2c5,6\U8282{\U7b2c1-8\U5468};\U534e\U5b87\U5b81;XX-130;;\U53ef\U7f16\U7a0b\U63a7\U5236\U5668;\U5468\U4e00\U7b2c5,6\U8282{\U7b2c9-16\U5468};\U66f2\U4e07\U6625;XX-336",
"\U667a\U80fd\U4eea\U5668\U4eea\U8868;\U5468\U4e00\U7b2c7,8\U8282{\U7b2c1-12\U5468};\U66f2\U4e07\U6625;XX-336",
"<null>",
"<null>"
);}而我现在制作的代码只能将Fri跟Mon放入TableView,代码如下:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"test";
NSString *testUrl = @"testurl";
NSURL *url = [NSURL URLWithString:testUrl];
//第二步,通过URL创建网络请求
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
NSData *JSONData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *jsonResult = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
self.str = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
self.data = jsonResult; NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"DictionaryJson is %@",dJSON);
NSMutableArray *_names=[[NSMutableArray alloc]init];
for (id item in jsonResult)
{
NSArray *array=[jsonResult valueForKey:item];
[_names addObject:[NSString stringWithFormat:@"%@ %@", item,joinedComponents]];;
self.names = _names;
NSLog(@"SELF.NAME IS:%@", self.names);
}
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.names count];
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 7.0 ];
cell.textLabel.font = myFont;
cell.textLabel.text = self.names[indexPath.row];
return cell;
}
我现在想要的效果是,将Json中用“,”分开的部分放在单独的TableViewCell里,如果这种不行,放在同一个TableViewCell的不同行里也是可以接受的。由于我没有办法对返回的Json进行修改,所以只能这么来了...求各位大神解答
NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];我的建议是再声明一个NSError对象,用于捕获转换失败的信息,如下
NSError *error = nil;
NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:&error];
if (error) {
NSLog(@"转换失败,error info:%@",error);
}还有一个就是你的代码中有一处有问题的地方, jsonResult 是NSData 类型,你代码中这段就有问题了
for (id item in jsonResult)
{
NSArray *array=[jsonResult valueForKey:item];
[_names addObject:[NSString stringWithFormat:@"%@ %@", item,joinedComponents]];;
self.names = _names;
NSLog(@"SELF.NAME IS:%@", self.names);
}
你实际需要操作的对象是,json转换成功后的NSDictionary的对象。这是一个字典,不能用for来遍历。只能是根据key来得到你想要取的值。