在 iOS 中理解并使用此 JSON 数据

发布于 2024-12-17 21:11:51 字数 1607 浏览 2 评论 0原文


我创建了一个返回 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听你说爱我 2024-12-24 21:11:51

问题是这样的:

NSArray *latestInvoice = [json objectForKey:@"invoice"];

实际上,它应该是:

NSDictionary *latestInvoice = [json objectForKey:@"invoice"];

......因为你拥有的是字典,而不是数组。

The problem is this:

NSArray *latestInvoice = [json objectForKey:@"invoice"];

In actual fact, it should be:

NSDictionary *latestInvoice = [json objectForKey:@"invoice"];

...because what you have is a dictionary, not an array.

榕城若虚 2024-12-24 21:11:51

哇,原生 JSON 解析器,竟然没有注意到它被引入了。

NSArray *latestInvoice = [json objectForKey:@"invoice"];

这实际上是一个 NSDictionary,而不是一个 NSArray。数组不会有键。从这里看来你很有能力。

Wow, native JSON parser, didn't even notice it was introduced.

NSArray *latestInvoice = [json objectForKey:@"invoice"];

This is actually a NSDictionary, not a NSArray. Arrays wont have keys. You seem capable from here.

坏尐絯 2024-12-24 21:11:51

在这里,我认为你必须像这样使用 nsdictionary

NSData* data = [NSData dataWithContentsOfURL: jsonURL];

NSDictionary *office = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSDictionary *invoice = [office objectForKey:@"invoice"];

Here I think You have to take to nsdictionary like this

NSData* data = [NSData dataWithContentsOfURL: jsonURL];

NSDictionary *office = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSDictionary *invoice = [office objectForKey:@"invoice"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文