[英]How do I parse this json response object?
I have a json response of the following type: 我有以下类型的json响应:
[{
cat_name : value
post : [{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
}]
},{
cat_name : value
post : [{
title:value
image:value
content:value
},
{
title:value
image:value
content:value
}]
},
{
cat_name : value
post : [{
title:value
image:value
content:value
}]
}]
and so on... 等等...
I've tried the following - 我尝试了以下方法-
Stored the response object in an array(responseArray). 将响应对象存储在数组(responseArray)中。 Implemented first for loop from 0 till responseArray.count, stored the value for post in an array (postArray).
首先实现从0到responseArray.count的for循环,将post的值存储在数组(postArray)中。 Inside the first loop I implemented the second loop from 0 till postArray.count and returned the value for count of postarray.count.
在第一个循环中,我实现了从0到postArray.count的第二个循环,并返回了postarray.count的count值。
EDIT - ADDED THE FOR LOOP 编辑-添加循环
for (int i = 0; i < responseArrayList.count; i++) {
postArrayList = [[responseArrayList objectAtIndex:i] valueForKey:@"post"];
for (int j = 0; j < postArrayList.count; j++) {
return postArrayList.count;
}
}
It gives a warning that the second loop wont be incremented and would only run once. 它发出警告,第二个循环不会增加,只会运行一次。
How do I set the return value for numberOfRowsInSection ? 如何设置numberOfRowsInSection的返回值?
Remove line from second for loop 从第二个循环中删除行
return postArrayList.count; 返回postArrayList.count;
If your method wants to return something then add return statement outside of for loop. 如果您的方法想要返回某些内容,请在for循环之外添加return语句。
@interface TableViewController ()
@property(nonatomic)NSArray *responseArrayList;
@end
Convert to JSON object and store it in your global variable 转换为JSON对象并将其存储在全局变量中
- (void)viewDidLoad {
[super viewDidLoad];
myJsonString = //here your JSON string response;
self.responseArrayList = [NSJSONSerialization JSONObjectWithData:[myJsonString dataUsingEncoding:NSUTF8StringEncoding]options:0 error:NULL];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.responseArrayList.count;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section{
NSArray *postArrayList = [[responseArrayList objectAtIndex:section] valueForKey:@"post"];
return postArrayList.count;
}
i suggest to write code like this... 我建议写这样的代码...
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.myarray=dict[@"result”][@“post”];
//store the value in newObject
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ my object is ",obj);
myobject *object = [[myobject alloc]init];
object.name = obj.productName;
object.image = obj.productimage;
[_myarray addObject:object];
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.