您好,我有一些問題解析IOS中的JSON。這是json數據。Json解析IOS中的錯誤
{
"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": "[email protected]",
"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<[email protected]>",
"sender_role": "admin",
"message_type": "broadcast",
"message": "parking space up for grabs",
"message_recipients": "all",
"reply_permitted": "0"
}
],
"events": [
{
"creator": "Travis<[email protected]>",
"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 value.According的結構,結構的人標記應返回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]);
+1這應該是正確的答案。一面說明;當解析JSON數據並期望數組時,您可能會發現令人討厭的驚喜,具體取決於JSON呈現(通常在服務器端)。對於這樣的JSON渲染器來說,它實際上提供了一個只包含單個對象而不是數組的對象。參見例如[PHP的json_encode](http://www.php.net/manual/en/function.json-encode.php)。 – Till
好點。我只是給他這個特殊例子的快速和骯髒的解析(沒有錯誤檢查/防守代碼)。 – Joel
謝謝喬爾。它的工作完美。我完全同意蒂爾。 – arindam