不解析PHP JSON
我不确定我的JSON或xcode中的代码是否有问题,但我无法让xcode解析我的JSON。我的JSON是有效的,但是我无法解析它。有人知道我做错了什么吗 以下是我用来生成JSON的PHP:不解析PHP JSON,php,ios,objective-c,json,Php,Ios,Objective C,Json,我不确定我的JSON或xcode中的代码是否有问题,但我无法让xcode解析我的JSON。我的JSON是有效的,但是我无法解析它。有人知道我做错了什么吗 以下是我用来生成JSON的PHP: $channel['items']['post']['category_name'][$category_name]['details'][] = array( 'title' => $title, 'description' => $description, 'cate
$channel['items']['post']['category_name'][$category_name]['details'][] = array(
'title' => $title,
'description' => $description,
'category_id' => $category_id,
);
}
$channels = array($channel);
$json = json_encode($channel);
header('Content-type: application/json');
echo $json;
这就产生了如下JSON:
{
"items": {
"post": {
"category_name": {
"Baby": {
"details": [
{
"title": "trying with category id again",
"price": "3344.55",
},
{
"title": "this is sending with description ",
"price": "900000.00",
}
]
},
“Other Baby Stuff": {
"details": [
{
"title": "putting in title",
"price": "3000.99",
这是我在xcode中要解析的代码(我希望我的节标题包含类别名称,如“Baby”、“其他Baby Stuff”等):
在我的tableview单元格中(我需要我的详细信息)
您在哪里解析JSON?您希望显示的数据到底是什么?给定的JSON是一个“JSON对象”(也称为字典、哈希、关联容器等)这不适合在表视图中显示。表视图需要一个对象数组。谢谢您的帮助。我想将其传递到我的表视图中。JSON对许多不同的类别重复。上面的一个只显示Baby,但我还有其他类别,如Baby Cloth等。因此每个类别名称都应该是m中的一个节名y表格,在表格的单元格中显示详细信息(标题等)。现在,对于各部分来说,这是有意义的。但您可能的意思是,每个详细信息条目都应该显示在一个单元格中(单独的行),而不是所有详细信息都显示在一个单元格中?哦,是的,所有详细信息都显示在一个单元格中(不是所有详细信息…只有部分和其他部分将显示在我的详细信息屏幕中)但我想你明白我的意思了。见楚坦20的答案。;)好的,谢谢。我正在尝试这个。你是说这会进入标题部分?NSString*部分..//好的,我同意你的观点。不幸的是,我妻子要我去购物。过一会儿我会试试这个。谢谢你的帮助
[[category allKeys]objectAtIndex:0]
将返回任意项,不一定是“Baby”,因为字典没有顺序。是的,因为他没有数组来保持顺序,所以我只使用索引0作为示例。当然,我说它将返回'Baby'作为值是错误的,但是从发布的示例json来看,更容易说'Baby'将是值。
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[m_tableView reloadData];
});
_buyCategory = json[@"items"][@"post"][@"category_name"];//THIS IS PARSING JUST FINE NOW
NSLog(@"Items: %@", _buyCategory);
NSLog(@"Array Count: %u", [_buyCategory count]);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (appDelegate.bLoadBuy == 1) {
NSString *section = [[_buyCategory allKeys] objectAtIndex:0]; ///NOT RETURNING ANYTHING
NSLog(@"category: %@", section); //I AM NOT EVEN GETTING NSLOG HERE
return [NSString stringWithFormat:@"%@",section];];
_buyTitle = [[tempResults objectForKey:@"details"] objectForKey:@"title"];
NSDictionary *category = json[@"items"][@"post"][@"category_name"];
//section title
NSString *section = [[category allKeys] objectAtIndex:0]
//array of detail object.. so details[0][@"title"] should give you the first titme
NSArray *details = [category objectForKey:section];