English 中文(简体)
分析和使用SOS中的这一JSON数据
原标题:Understand and use this JSON data in iOS

我创建了一个网络服务,我这样认为。 退还的数据类似:

{"invoice":{"id":44,"number":42,"amount":1139.99,"checkoutStarted":true,"checkoutCompleted":true}}

对我来说,这看起来像是有效的智者。

在5世纪里,我使用本土的JSON序列izer,把数据作为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数据具有“发票”关键特性,其价值为NSArray <代码>(imount = ......,eck...})。

因此,我把价值转换为国家航空航天局:

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

但是,当步行时,它说,最近的是 发票不是非洲法郎。 如果我打印阵列中的数值:

for (id data in latestInvoice) {
        NSLog(@"data is %@", data);
    }

结果是:

data is id
data is checkoutStarted
data is ..

我不理解为什么它只归还“id”而不是“id = 44”。 如果我把JSON数据确定给NSDictionary,我知道钥匙是NSString,但价值是什么? 是不是国家安全局,还是什么?

This is the tutorial that I read: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

Edit:从答案看,看来,NSDictionary *json是另一个NSDictionary。 我假定,这是错误的NSArray或NSString。 换言之,NSDictionary *json = [@“invoice”, NSDictionary]

最佳回答

The problem is this:

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

实际上,应当:

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

......因为你是一个独裁者,而不是一个阵列。

问题回答

Wow, 本土的JSON parser, 甚至没有通知。

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

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

在这方面,我 你们不得不这样做。

NSData* data = [NSData dataWithContentsOfURL: jsonURL];

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

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




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签