我有来自数组的jsonString,它看起来像这样:
NSMutableArray *tempListArray = [NSMutableArray new];
for (VideoListData *obj in self.listArray) {
NSMutableDictionary *dict = [obj convertotDict:obj];
[tempListArray addObject:dict];
}
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempListArray options:0 error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
它包含json格式的数据:
[
{
"ImageUrl":"...",
"Duration":0,
"PlaylistId":"",
"Content":"...",
"LastPlayTime":0,
"VideoId":"5"
},
{
"ImageUrl":"...",
"Duration":0,
"PlaylistId":"",
"Content":"...",
"LastPlayTime":0,
"VideoId":"1959"
}
]
如何只过滤关键字" VideoId"并在另一个新的jsonString?或者以数组形式过滤它更好吗?所以看起来应该是这样的:
[
{
"VideoId":"5"
},
{
"VideoId":"1959"
}
]
答案 0 :(得分:0)
你已经在数组中拥有它了。所以通常访问索引值就像。首先获取ID,然后如果要进一步使用它,创建一个字典,每次添加MyVideoId并在数组中添加每个字典。首先得到VideoId。
试一试:
NSString *MyVideoId=[[tempListArray objectAtIndex:0] objectForKey:@"VideoId"];
或者,如果您希望它来自当前状态,请尝试:
NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
NSString *MyVideoId=[[jsonObject objectAtIndex:0] objectForKey:@"VideoId"];
你会添加喜欢,你会在循环内。在循环内部循环和tempDict之外声明myNewarra。
NSMutableArray *myNewarra=[[NSMutableArray alloc] init];
NSDictionary *tempDict=[[NSDictionary alloc] init];
[tempDict setValue:MyVideoId forKey:@"VideoId"];
[myNewarra addObject:tempDict];