試圖從json中獲取值,顯示正確獲取數據的響應字符串,但是當NSData轉換並將其放入NSDictionary中時,兩個值會互換。 下面是代碼嘗試。從JSON獲取NSData的奇怪響應
+(NSMutableDictionary *)seatMap:(NSDictionary *)seatId eventId:(NSDictionary *)eid
{
NSString *urlStr = [NSString stringWithFormat:@"http://met.co/api/seatmap/%@/%@", [seatId valueForKey:@"sid"], [eid valueForKey:@"eid"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:
[urlStr stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]]];
//NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlStr]];
NSString *responseString = [MetApi sendRequest:request];
NSLog(@"response:%@", responseString);
NSError *error;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF16StringEncoding];
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
//NSDictionary *results = [responseString JSONValue];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:results];
NSLog(@"dict in API-------------%@",dict);
return dict;
}
以上代碼給這個輸出
1 = off;
10 = off;
2 = on;
3 = on;
4 = on;
5 = on;
6 = on;
7 = on;
8 = on;
9 = on;
但應該
1: "off",
2: "on",
3: "on",
4: "on",
5: "on",
6: "on",
7: "on",
8: "on",
9: "on",
10: "off"
JSON文件
{
row1: {
1: "on",
2: "on",
3: "on",
4: "on",
5: "on",
6: "on",
7: "on",
8: "on",
9: "on",
10: "on",
attr: {
total: "10",
type: "Gold"
}
},
row2: {
1: "off",
2: "on",
3: "on",
4: "on",
5: "on",
6: "on",
7: "on",
8: "on",
9: "on",
10: "off",
attr: {
total: "10",
type: "Gold"
}
}
}
}
爲什麼這個交換數據的情況發生。請指導以上內容,如有不清楚的地方請提問。 在此先感謝。
得到某種答案這裏的NSDictionary
objectForKey:
方法的價值。 [1]:http://stackoverflow.com/questions/16913847/sort-nsdictionary-in-ascending-order/16915518?noredirect=1#16915518 –