gpt4 book ai didi

ios - 解析嵌套的json数组

转载 作者:行者123 更新时间:2023-12-01 22:29:56 25 4
gpt4 key购买 nike

我需要解析以下嵌套的json。

{  
"status":1,
"value":{
"details":[
{
"text":"this is test",
"company":"General Marketing Company",
"date":"05-DEC-15"
},
{
"text":"this is test2",
"company":"NJ, Chennai",
"date":"05-DEC-15"
},
{
"text":"Sample test message for welcome",
"company":"sd",
"date":"22-JAN-16"
}
]
}
}

以下是我的代码,
NSString* testimonialResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSData *jsonData = [testimonialResponse dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSLog(@"Response %@",jsonDic[@"value"][@"details"]);

上面的代码实际上通常可以解析细节中的内容,但不能唯一地解析每个数据。例如,我的代码并没有唯一地列出所有公司名称,文本,日期。

最佳答案

喜欢

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSArray *dataArray = jsonDic[@"value"][@"details"];

for (NSDictionary *tmp in dataArray)
{
NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[tmp objectForKey:@"text"] forKey:@"text"];
[temp setObject:[tmp objectForKey:@"company"] forKey:@"company"];
[temp setObject:[tmp objectForKey:@"date"] forKey:@"date"];

[yourArrayName addObject:temp];

}

EDit

如果您想在这里停留,请使用此
NSArray * finalAray = [NSArray arrayWithArray: jsonDic[@"value"][@"details"]]

关于ios - 解析嵌套的json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com