iOS使用解析Json并放入TableView

时间:2022-10-24 15:03:08

I'm very new to both Obj-c and json, I'm creating my first iOS project using a TableViewController. The json I got from the WCF looks like this:

我对Obj-c和json都很陌生,我正在使用TableViewController创建我的第一个iOS项目。我从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>"
);}

And what I did with this Json is this:

我对这个Json做的是这样的:

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"JSONRead";
NSString *testUrl = @"testurl";
NSURL *url = [NSURL URLWithString:testUrl];

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)
    [_names addObject:[NSString stringWithFormat:@"%@", item]];;
self.names = _names;
NSLog(@"%@", self.names);

   }

And what comes into this NSMutableArray names is only Fri and Mon that has showed in the TableView. What I wanna ask is, how to show the other information in the tableview as well? Or how to add all those info into the NSMutableArray names? Like this form, those lines in different TableView Cells, not in the same one

而这个NSMutableArray名称只有在TableView中显示的Fri和Mon。我想问的是,如何在tableview中显示其他信息?或者如何将所有这些信息添加到NSMutableArray名称中?就像这个表单一样,那些行在不同的TableView单元格中,而不是在同一个表格中

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

......

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

......

Thanks very much, I would post pics if I could, but I can't...Sorry about that...and Thanks for the answers

非常感谢,如果可以,我会发布图片,但我不能...对此感到抱歉...并且感谢您的回答

Also, I didn't mean that those xxx,xxxx to show in one row... I want them to show in different TableViewCells if possible, or show in different rows in one cells if there's no better solution. So adding them into a single NSString isn't the solution I'm looking for... Sorry

另外,我并不是说那些xxx,xxxx在一行中显示...我希望它们在可能的情况下显示在不同的TableViewCells中,或者如果没有更好的解决方案则在一个单元格中显示不同的行。所以将它们添加到单个NSString中并不是我正在寻找的解决方案......抱歉

1 个解决方案

#1


0  

Try below solution..

试试以下解决方案

for (id item in jsonResult)
    {
    NSArray *array=[jsonResult valueForKey:item];
    NSString *joinedComponents = [array componentsJoinedByString:@","];
    [_names addObject:[NSString stringWithFormat:@"%@ %@", item,joinedComponents]];;
    self.names = _names;
    NSLog(@"%@", self.names);
    }

Hope it helps you..!

希望它能帮助你......!

#1


0  

Try below solution..

试试以下解决方案

for (id item in jsonResult)
    {
    NSArray *array=[jsonResult valueForKey:item];
    NSString *joinedComponents = [array componentsJoinedByString:@","];
    [_names addObject:[NSString stringWithFormat:@"%@ %@", item,joinedComponents]];;
    self.names = _names;
    NSLog(@"%@", self.names);
    }

Hope it helps you..!

希望它能帮助你......!