gpt4 book ai didi

ios - 如何删除 iOS 中 NSString 中的第一个和最后一个 {}?

转载 作者:行者123 更新时间:2023-11-29 12:09:10 24 4
gpt4 key购买 nike

我想从我的 NSString 中删除第一个和最后一个括号。

{
"Questions" : [
{
"title" : "This is my Question",
"question_id" : "123123123213",
"answers" : [
"correct answer 1",
"wrong answer 1",
"wrong answer 2",
"wrong answer 3"
],
"media_type" : "",
},
{
"title" : "This is my Question",
"question_id" : "2342342342342",
"answers" : [
"correct answer 1",
"wrong answer 1",
"wrong answer 2",
"wrong answer 3"
],
"media_type" : "",
}
]
}

我只想从上面的 NSString 中删除第一个和最后一个 { }

确保它不是字典。我在 NSString 中有这个。

最佳答案

更新

选项是序列化:

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dictJson = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:nil];

NSArray *arr = dictJson[@"Questions"];

for (NSDictionary *dict in arr)
{
NSLog(@"title ==%@",dict[@"title"]);
.....
}

更新 2

但是要小心你的字符串。这是不对的。您必须检查 media_type 之后的逗号:

{
"Questions" : [
{
"title" : "This is my Question",
"question_id" : "123123123213",
"answers" : [
"correct answer 1",
"wrong answer 1",
"wrong answer 2",
"wrong answer 3"
],
"media_type" : ""**,** //THIS
},
{
"title" : "This is my Question",
"question_id" : "2342342342342",
"answers" : [
"correct answer 1",
"wrong answer 1",
"wrong answer 2",
"wrong answer 3"
],
"media_type" : ""**,** //AND THIS
}
]
}

那里没有逗号。

关于ios - 如何删除 iOS 中 NSString 中的第一个和最后一个 {}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001003/

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