我正在研究检索JSON数据的API,如下所示:
{"status":1,
"notification":
[
{
"id":"175",
"from_user":"3",
"to_user":"7",
"content":"Azri invited you to join the Kaptr.",
"is_read":"0",
"type":"20",
"created_date":"2014-04-17 04:11:02",
"custom_prop":"{"status":1,
"api_name":"event_invite",
"event_id":155,
"event_name":false,
"invitee":"7"
}",
"from_user_avatar_url":"http:\/\/app.kaptr.co\/vs\/kaptr_app\
/images\/profile\/3\/1cAR7QSu.jpg"
}
]
}
我想在event_id
对象下获取custom_prop
。我有什么办法可以解决这个问题。
答案 0 :(得分:-1)
试试这个:
NSString *da = [NSString stringWithFormat:@"{\"status\": \"1\",\"notification\": [{\"id\": \"175\",\"from_user\": \"3\",\"to_user\": \"7\",\"content\": \"Azri invited you to join the Kaptr.\",\"is_read\": \"0\",\"type\": \"20\",\"created_date\": \"2014-04-17 04:11:02\",\"custom_prop\": [{\"status\": \"1\",\"api_name\": \"event_invite\",\"event_id\": \"155\",\"event_name\": \"false\",\"invitee\": \"7\"}],\"from_user_avatar_url\": \"string UTl\"}]}"];
NSData* data1 = [da dataUsingEncoding:NSUTF8StringEncoding];
NSString * strdata =[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"responseone %@", strdata);
NSError *e = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&e];
NSArray *myArray = [json objectForKey:@"notification"];
for (NSDictionary *dict in myArray)
{
NSArray *myArray2 = [dict objectForKey:@"custom_prop"];
for (NSDictionary *dict in myArray2)
{
NSString *eventId = [dict objectForKey:@"event_id"];
NSLog(@"eventId = %@", eventId);
}
}
答案 1 :(得分:-1)
所以我不是来自.net的Objective-c世界,但是我猜测解决方案是一样的,你有2个解决这个问题的方法: 1.创建一个代表这个JSON的对象,如:
class SomeClass{
public string Status {get;set;}
public NotificationEnt[] {get;set;}
}
class NotificationEnt{
public CustomProp {get;set;}
}
class CustomProp {
public string event_Id {get;set;}
}
并使用您的JSON序列化对象对其进行序列化(我确定您有一些东西)
或 2.将其序列化为动态对象(我不喜欢这种方法,因为它会离开你 有充足的例外和错误空间。)
希望这有助于您朝着正确的方向前进。