在 iOS 中理解并使用此 JSON 数据
我创建了一个返回 JSON 的 Web 服务,我认为是这样。返回的数据如下所示:
{"invoice":{"id":44,"number":42,"amount":1139.99,"checkoutStarted":true,"checkoutCompleted":true}}
对我来说,这看起来像有效的 JSON。
使用 iOS5 中的原生 JSON 序列化程序,我获取数据并将其捕获为 NSDictionary。
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];
NSLog(@"json count: %i, key: %@, value: %@", [json count], [json allKeys], [json allValues]);
日志的输出是:
json count: 1, key: (
invoice
), value: (
{
amount = "1139.99";
checkoutCompleted = 1;
checkoutStarted = 1;
id = 44;
number = 42;
}
)
所以,在我看来,JSON 数据有一个 NSString 键“invoice”,其值为 NSArray ({amount = ..., check...})
因此,我将这些值转换为 NSArray:
NSArray *latestInvoice = [json objectForKey:@"invoice"];
但是,在单步执行时,它表示latestInvoice 不是 CFArray。如果我打印出数组内的值:
for (id data in latestInvoice) {
NSLog(@"data is %@", data);
}
结果是:
data is id
data is checkoutStarted
data is ..
我不明白为什么它只返回“id”而不是“id = 44”。如果我将 JSON 数据设置为 NSDictionary,我知道键是 NSString 但值是多少?是 NSArray 还是其他什么?
这是我读过的教程: http://www.raywenderlich.com/5492/working-with-json-in-ios-5
编辑:从答案,似乎 NSDictionary *json 的“值”是另一个 NSDictionary。我认为它是 NSArray 或 NSString 这是错误的。换句话说,[K,V] for NSDictionary *json = [@"invoice", NSDictionary]
I created a web service which returns JSON or so I think. The data returned look like this:
{"invoice":{"id":44,"number":42,"amount":1139.99,"checkoutStarted":true,"checkoutCompleted":true}}
To me, that looks like valid JSON.
Using native JSON serializer in iOS5, I take the data and capture it as a NSDictionary.
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];
NSLog(@"json count: %i, key: %@, value: %@", [json count], [json allKeys], [json allValues]);
The output of the log is:
json count: 1, key: (
invoice
), value: (
{
amount = "1139.99";
checkoutCompleted = 1;
checkoutStarted = 1;
id = 44;
number = 42;
}
)
So, it looks to me that the JSON data has a NSString key "invoice" and its value is NSArray ({amount = ..., check...})
So, I convert the values to NSArray:
NSArray *latestInvoice = [json objectForKey:@"invoice"];
But, when stepping through, it says that latestInvoice is not a CFArray. if I print out the values inside the array:
for (id data in latestInvoice) {
NSLog(@"data is %@", data);
}
The result is:
data is id
data is checkoutStarted
data is ..
I don't understand why it only return the "id" instead of "id = 44". If I set the JSON data to NSDictionary, I know the key is NSString but what is the value? Is it NSArray or something else?
This is the tutorial that I read:
http://www.raywenderlich.com/5492/working-with-json-in-ios-5
Edit: From the answer, it seems like the "value" of the NSDictionary *json is another NSDictionary. I assume it was NSArray or NSString which is wrong. In other words, [K,V] for NSDictionary *json = [@"invoice", NSDictionary]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是这样的:
实际上,它应该是:
......因为你拥有的是字典,而不是数组。
The problem is this:
In actual fact, it should be:
...because what you have is a dictionary, not an array.
哇,原生 JSON 解析器,竟然没有注意到它被引入了。
这实际上是一个 NSDictionary,而不是一个 NSArray。数组不会有键。从这里看来你很有能力。
Wow, native JSON parser, didn't even notice it was introduced.
This is actually a NSDictionary, not a NSArray. Arrays wont have keys. You seem capable from here.
在这里,我认为你必须像这样使用 nsdictionary
Here I think You have to take to nsdictionary like this