【问题标题】:Strange response from NSData fetching from JSON从 JSON 获取的 NSData 的奇怪响应
【发布时间】:2013-06-04 05:07:56
【问题描述】:

尝试从 json 中获取值,响应字符串显示正确获取的数据,但是当 NSData 转换并将其放入 NSDictionary 时,两个值会互换。 下面是尝试过的代码。

 +(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"
}
}
}
}

为什么会发生这种数据交换。以上请指教,如有不明白请追问。 提前致谢。

【问题讨论】:

标签: iphone json


【解决方案1】:

您的 JSON 仅包含字典,而字典不保持顺序。换句话说,打印字典时元素的顺序完全是特定于实现的,甚至可能是随机的。

如果顺序相关,您必须使用列表(例如[1,2,3]

例如,您可以使用以下 JSON

{ "rows":
  [ { "values":["on", "on", "on", "on", "on", "on", "on", "on", "on", "on"]
    , "attr": { "total": 10
              , "type": "Gold"
              }
    }
,   { "values":["off", "on", "on", "on", "on", "on", "on", "on", "on", "off"]
    , "attr": { "total": 10
              , "type": "Gold"
              }
    }
  ]
}

如果您不想更改 JSON 并获取值,例如 row1,您可以使用以下 sn-p(不推荐,如果顺序很重要,请使用列表!):

/* expects `NSDictionary *dict` as defined in the code of your question. Code is untested but should give you the idea... */
NSInteger max = [dict[@"row1"][@"attr"][@"total"] intValue];

for (int i=1; i<= max; i++) {
    NSLog("value %ld = '%@'", i, dict[@"row1"][@"attr"][[NSString stringWithFormat:@"%d",i]]);
}

【讨论】:

  • 好的,但是没有任何解决方案可以通过编程方式来处理我的代码。
  • 有什么问题?我猜问题是顺序是“1, 10, 2, 3, 4, 5, 6, 7, 8, 9” 而不是“1, 2, 3, 4, 5, 6, 7, 8, 9 , 10"
  • 你到底想达到什么目标?
  • 你是对的,你猜对了问题。但是您建议更改 json,我只是问有没有办法以编程方式处理这件事,而不是更改 json。
  • 查看编辑,也许这涵盖了您想要的内容。否则指定问题。我仍然不明白您想要实现什么,这对于帮助您找到解决方案至关重要。
【解决方案2】:

NSDictionary 键值对很重要而不是它的出现顺序

即使订单发生变化,也可以使用objectForKey:方法获取值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多