【发布时间】:2017-03-02 08:15:00
【问题描述】:
我需要解析一个 json 并以与它们在响应中出现的顺序相同的顺序获取键值对。
目前我正在做的是
-(instancetype)initWithJson:(NSDictionary *)responseDict {
if (self = [super init]){
NSArray *tempArray = [[NSArray alloc] init];
NSArray *roomSizesForAcArray = [responseDict valueFromDictionaryWithNSNullCheck:@"roomSizesForAc"];
NSArray *loadChartForInverterArray = [responseDict valueFromDictionaryWithNSNullCheck:@"loadChartForInverter"];
if(roomSizesForAcArray && roomSizesForAcArray.count>0){
self.isInverterChart=false;
tempArray=roomSizesForAcArray;
}
else if(loadChartForInverterArray && loadChartForInverterArray.count>0){
self.isInverterChart=true;
tempArray=loadChartForInverterArray;
}
self.arrayOfChartSizeObjects=tempArray;
if(tempArray && tempArray.count>0){
//Temp array first object is a dictionary which i need to parse sequentially
self.arrayOfKeys = [[tempArray objectAtIndex:0] allKeys];
//This array of keys is random every time and not sequential
}
}
return self;
}
我需要以某种方式解析字典 [tempArray objectAtIndex:0] 维护 init 中键的顺序。
【问题讨论】:
-
为什么它们在你的字典中的顺序对你很重要?只需使您从 dict 中提取值的顺序符合您的要求。
标签: ios json nsdictionary