gpt4 book ai didi

ios - 为数组创建循环以从 json 字符串获取字符串?

转载 作者:行者123 更新时间:2023-11-29 02:24:19 25 4
gpt4 key购买 nike

我需要显示一个 TableView ,其中包含来自 Web 服务响应的信息,我没有在此处做错我的示例代码

    NSData *data = [soapResultsString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSArray *array = [json allValues];

for (int i=0; i<array.count; i++)
{
recordResults =NO;
appDelegate.rateString =[[[json valueForKey:@"plan_history"]valueForKey:@"rate"]objectAtIndex:i];
appDelegate.descriptionString=[[[json valueForKey:@"plan_history"]valueForKey:@"description"]objectAtIndex:i];
appDelegate.validityString=[[[json valueForKey:@"plan_history"]valueForKey:@"validity"]objectAtIndex:i];
appDelegate.plantypeString=[[[json valueForKey:@"plan_history"]valueForKey:@"plantype"]objectAtIndex:i];

}

我需要解析 plan_history 中的 4 个值,例如“rate”、“description”、“validity”、“plan type”当我运行我的应用程序时,我在 TableView 中只得到一组值。即我的 json 字符串包含超过 20 条记录,包含费率、描述、有效性和计划类型
你能告诉我如何循环我的 json 值并在 TableView 中显示我的所有记录吗

最佳答案

您应该消除对 allValuesvalueForKey 的调用,因为重复调用这些方法是处理 JSON 解析的非常低效的方法。

在您的一条评论中,您说您的 JSON 看起来像:

{
"plan_history": [
{
"rate": "₹1000",
"description": "FullTalktimeTopupRs.1000FullTalktime",
"validity": "Validity: 0\r",
"plantype": "FullTalkTime"
},
{
"rate": "₹508",
"description": "FullTalktimeTopupRs.558morethanFullTalktime",
"validity": "Validity: 2\r",
"plantype": "FullTalkTime"
}
]
}

(我想知道在给定您的 allValues 引用的此 plan_history 条目之前是否有内容,但除非您另有说明,否则我会假设这是原始 JSON看起来像。)

如果是这样,要解析它你会这样做:

NSMutableArray *results = [NSMutableArray array];

NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *planHistory = json[@"plan_history"];

for (NSDictionary *planHistoryEntry in planHistory) {
NSString *rateString = planHistoryEntry[@"rate"];
NSString *description = planHistoryEntry[@"description"];
NSString *validity = planHistoryEntry[@"validity"];
NSString *planType = planHistoryEntry[@"plantype"];

// now do whatever you want with these four values.

// for example, I'd generally create a custom object I defined elsewhere for these four values and add to results, e.g.

[results addObject:[PlanHistoryEntry planHistoryEntryWithRate:rateString
description:description
validity:validity
planType:planType]];
}

// now do something with results, e.g. store it in some property in `appDelegate`, etc.

其中,PlanHistoryEntry 可以这样定义:

@interface PlanHistoryEntry : NSObject

@property (nonatomic, copy) NSString *rateString;
@property (nonatomic, copy) NSString *planDescription; // note, do not use `description` for property name
@property (nonatomic, copy) NSString *validity;
@property (nonatomic, copy) NSString *planType;

+ (instancetype) planHistoryEntryWithRate:(NSString *)rateString
planDescription:(NSString *)planDescription
validity:(NSString *)validity
planType:(NSString *)planType;

@end

@implementation PlanHistoryEntry

+ (instancetype) planHistoryEntryWithRate:(NSString *)rateString
planDescription:(NSString *)planDescription
validity:(NSString *)validity
planType:(NSString *)planType
{
PlanHistoryEntry *entry = [[self alloc] init];
entry.rateString = rateString;
entry.planDescription = planDescription;
entry.validity = validity;
entry.planType = planType;

return entry;
}

@end

但我不想让您迷失在这个答案的 Twig 末节中(因为考虑到这个问题的模棱两可性,我可能在一些细节上弄错了)。关键是您不应该使用 allValuesvalueForKey。如上所示,只需更直接地导航 JSON 结构即可。

关于ios - 为数组创建循环以从 json 字符串获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740497/

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