[英]How to parse NSDictionary sequentially?
I need to parse a json and get the key value pairs in the same sequence as they are present in response. 我需要解析一个json并以与响应中存在的键值对相同的顺序获取它们。
Currently what i'm doing is 目前我正在做的是
-(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;
}
I need to someway parse the dictionary [tempArray objectAtIndex:0] maintaining the order of keys in init. 我需要以某种方式解析字典[tempArray objectAtIndex:0],以维护init中的键顺序。
Not clear what you want, you want dictionary while you are already parsing the dictionary. 不清楚要什么,想要字典,而您已经在解析字典。 To get dictionary from the JSON use below code.
要从JSON获取字典,请使用以下代码。
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:receiveData options:kNilOptions error:&error];
To get all the keys and then retrieve the details use 要获取所有密钥,然后检索详细信息,请使用
NSArray *sortedKeys = [[jsonDict allKeys]
Once you have keys then get the details 拥有钥匙后,即可获取详细信息
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
Dictionaries are unordered collections. 字典是无序集合。 You say "...maintaining the order of keys in init".
您说“ ...维护init中的键顺序”。 There is no such thing.
哪有这回事。
JSON "Objects", the JSON equivalent of a NSDictionaries/Swift Dictionaries, have the same issue. JSON“对象”,即NSDictionaries / Swift字典的JSON等效项,具有相同的问题。
Some JSON libraries will preserve the order in which key/value pairs are submitted to be sent, but you should not depend on that. 某些JSON库将保留键/值对的发送顺序,但是您不应依赖于此。 The JSON protocol does not guarantee it.
JSON协议不保证它。
Once you receive JSON data and convert it to an NSDictionary
/ Dictionary
, the order in which the key/value pairs is sent is lost. 接收JSON数据并将其转换为
NSDictionary
/ Dictionary
,键/值对的发送顺序就会丢失。 The only way I know of to preserve the (already unreliable) order of the key/value pairs from the original JSON data stream is to parse the JSON yourself rather than deserializing it using NSJSONSerialization
. 我知道要保留原始JSON数据流中键/值对的(已经不可靠)顺序的唯一方法是自己解析JSON,而不是使用
NSJSONSerialization
反序列化它。
If you want your data in a particular order, you should use an array as the container object to send it. 如果要按特定顺序排列数据,则应使用数组作为容器对象来发送数据。 You can send an array of key/value pairs if you need to.
您可以根据需要发送一组键/值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.