0

嗨,我在 IOS 中解析 json 时遇到了一些问题。这是json数据。

{
"app_info": [
    {
        "app_name": "haka",
        "sync_protocol_version": "1"
    }
],
"updates": [
    {
        "timestamp": "Sat Apr 21 13:04:08 IST 2012",
        "people": [
            {
                "personal_info": [
                    {
                        "first_name": "phlox",
                        "last_name": "",
                        "employee_id": "010",
                        "gender": "-",
                        "marital_status": "-",
                        "nationality": "Denobulan",
                        "dob": "re-23",
                        "photo": "http://c.cc/users/010/profile/image"
                    }
                ],
                "contact_details": [
                    {
                        "address": [
                            {
                                "street": "#1, this way",
                                "city": "tank",
                                "state": "sick bay",
                                "zip": "0978",
                                "country": "Enterprise"
                            }
                        ],
                        "telephone": [
                            {
                                "work": "010",
                                "mobile": "010",
                                "home": "010"
                            }
                        ],
                        "email": [
                            {
                                "work": "phlox@nx-10.ent",
                                "personal": ""
                            }
                        ]
                    }
                ],
                "emergency": [
                    {
                        "contact": [
                            {
                                "name": "-",
                                "relationship": "",
                                "telephone": [
                                    {
                                        "work": "",
                                        "home": "",
                                        "mobile": ""
                                    }
                                ]
                            }
                        ],
                        "blood_group": ""
                    }
                ],
                "categorization": [
                    {
                        "designation": "",
                        "department": "",
                        "location": "",
                        "joining_date": ""
                    }
                ]
            },
            {
                "personal_info": [
                    {
                        "first_name": "",
                        "last_name": "",
                        "employee_id": "",
                        "gender": "",
                        "marital_status": "",
                        "nationality": "",
                        "dob": "",
                        "photo": ""
                    }
                ],
                "contact_details": [
                    {
                        "address": [
                            {
                                "street": "",
                                "city": "",
                                "state": "",
                                "zip": "",
                                "country": ""
                            }
                        ],
                        "telephone": [
                            {
                                "work": "",
                                "mobile": "",
                                "home": ""
                            }
                        ],
                        "email": [
                            {
                                "work": "",
                                "personal": ""
                            }
                        ]
                    }
                ],
                "emergency": [
                    {
                        "contact": [
                            {
                                "name": "",
                                "relationship": "",
                                "telephone": [
                                    {
                                        "work": "",
                                        "home": "",
                                        "mobile": ""
                                    }
                                ]
                            }
                        ],
                        "blood_group": ""
                    }
                ],
                "categorization": [
                    {
                        "designation": "",
                        "department": "",
                        "location": "",
                        "joining_date": ""
                    }
                ]
            }
        ],
        "messages": [
            {
                "sender": "Archer<admin@nx-10.ent>",
                "sender_role": "admin",
                "message_type": "broadcast",
                "message": "parking space up for grabs",
                "message_recipients": "all",
                "reply_permitted": "0"
            }
        ],
        "events": [
            {
                "creator": "Travis<ensign@nx-01.ent>",
                "event_title": "",
                "event_description": "",
                "event_time_start": "",
                "event_time_end": "",
                "location": "",
                "invitees": [
                    {
                        "id": "020",
                        "acceptance": "1"
                    }
                ]
            }
        ],
        "settings": [
            {
                "sync_frequency": "0"
            }
        ]
    }
]}

这是一个有效的 json 格式。我已经使用http://jsonlint.com/并使用http://braincast.nl/samples/jsoneditor/检查了它

查看 json 值的结构。根据结构,人员标签应返回计数 2,因为它有 2 个对象,但在解析时只返回 1。我完全没有选择。请帮助各位。

这是我用来解析的代码

NSString *textPAth = [[NSBundle mainBundle] pathForResource:@"sync" ofType:@"json"];
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:textPAth encoding:NSUTF8StringEncoding error:&error];    
NSArray *jsonArray = [[content JSONValue] retain]; 
NSLog(@"JSON ARRAY   %@   AND COUNT  %d",[[jsonArray valueForKey:@"updates"] valueForKey:@"people"],[[[jsonArray valueForKey:@"updates"] valueForKey:@"people"]  count]);
4

2 回答 2

2

我不确定为什么每个字典都用一个数组包装,当只有 1 个条目(不必要)时,但如果你想解析它,你会想要这样的东西:

NSDictionary *jsonDict = [[content JSONValue] retain];
NSArray *updatesArray = [jsonDict objectForKey:@"updates"];
NSDictionary *updatesDict = [updatesArray objectAtIndex:0];
NSArray *peopleArray = [updatesDict objectForKey:@"people"];
NSLog(@"People count: %i", [peopleArray count]);
for (NSDictionary *peopleDict in peopleArray) {
    //do something
}

作为旁注,如果这是您的 JSON,您将需要删除实际上不是数组的条目的 [arrays]。虽然您仍然可以解析它,但这意味着您必须先读取数组,然后从 objectAtIndex:0 获取字典,这完全是低效且毫无意义的。

于 2012-05-02T21:04:16.423 回答
0

会不会是您的每个对象都包装在一个数组中并给出了意想不到的结果?

于 2012-05-02T21:01:53.990 回答