我有以下JSON响应的摘录:
"em_phone": "987654321",
"stage": 2,
"stage_count": 2,
"allowed_staff": 1,
"expected_staff": null,
"active_staff": 0,
"url": "/api2/jobs/508190271",
"user": {
"owned": false,
"contractor": false,
"pending": false,
"will_block": false,
"blocking": false,
"writable": true,
"flow": {
"blind": false
}
我如何从“用户”访问“承包商”?
读取JSON的代码:
for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:@"id"];
NSString *author_data = [dataDict objectForKey:@"user"];
NSString *thumbnail_data = [dataDict objectForKey:@"created_at"];
问题在于以下几行:
NSString *author_data = [dataDict objectForKey:@"user"];
感谢。
答案 0 :(得分:1)
:“user”是一本字典。将您的行更改为
NSDictionary *author_data = [dataDict objectForKey:@"user"];
并访问数据[author_data objectForKey:@"contractor"]
答案 1 :(得分:0)
[dataDict objectForKey:@"user"]
将返回另一个NSDictionary。
你应该写一些类似的东西:
NSDictionary *user = [dataDict objectForKey:@"user"];
然后:
BOOL isContractor = [[user objectForKey:@"contractor"] boolValue];
答案 2 :(得分:0)
过去一周我一直在研究这个问题。我决定写自己的解决方案。它非常简单,并且基于现有的Apple功能。
见这里:https://github.com/gslinker/GSObject
在这里:http://digerati-illuminatus.blogspot.com/2016/01/objective-c-and-json-convert-subclass.html
对于您的数据模型对象,它继承自GSObject而不是NSObject。以下是继承自GSObject的ThingOne示例:
ThingOne* object1 = [[ThingOne alloc] init];
object1.name = @"John Jones";
NSData* jsonData1 = [object1 toJsonDataWithOptions:NSJSONWritingPrettyPrinted];
NSString *jsonString1 = [object1 toJsonStringWithOptions:NSJSONWritingPrettyPrinted];
NSDictionary<NSString *,id> *dict1 = [GSObject dictionaryWithValues:object1];
NSString *roundTripJson1 = [object1 toJsonStringWithOptions:NSJSONWritingPrettyPrinted];
//
// ThingOne.h
// JasonStuff
//
// Created by Geoffrey Slinker on 12/28/15.
// Copyright © 2015 Slinkworks LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GSObject.h"
#import "ThingTwo.h"
@interface ThingOne : GSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) ThingTwo *thingTwo;
@property (nonatomic, retain) NSArray *values;
@property (nonatomic, retain) NSDictionary *dict;
@property int myInt;
@property float myFloat;
@property BOOL myBool;
@property (nonatomic, retain) NSNumber* someMoney;
@end
//
// ThingOne.m
// JasonStuff
//
// Created by Geoffrey Slinker on 12/28/15.
// Copyright © 2015 Slinkworks LLC. All rights reserved.
//
#import "ThingOne.h"
@implementation ThingOne
@synthesize name;
@synthesize thingTwo;
@synthesize values;
@synthesize dict;
@synthesize myInt;
@synthesize myFloat;
@synthesize myBool;
@synthesize someMoney;
- (instancetype)init
{
self = [super init];
thingTwo = [[ThingTwo alloc] init];
thingTwo.stuff = @"Thing Two Stuff";
thingTwo.someOtherStuff = @"Thing Two Other Stuff";
NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
[dateFormater setDateFormat:@"yyyy-mm-dd"];
thingTwo.someDate = [dateFormater dateFromString:@"1963-10-07"];
values = [NSArray arrayWithObjects:@"Value1", @"Value2", @"Value3", nil];
dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
myInt = 5431;
myFloat = 123.456f;
myBool = YES;
someMoney = [NSNumber numberWithInt:503];
return self;
}
@end
//
// ThingTwo.h
// JasonStuff
//
// Created by Geoffrey Slinker on 12/28/15.
// Copyright © 2015 Slinkworks LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GSObject.h"
@interface ThingTwo : GSObject
@property (nonatomic, retain) NSString *stuff;
@property (nonatomic, retain) NSString *someOtherStuff;
@property (nonatomic, retain) NSDate *someDate;
@property (nonatomic, retain) NSString *nullString;
@property (nonatomic, retain) NSDate *nullDate;
@end
//
// ThingTwo.m
// JasonStuff
//
// Created by Geoffrey Slinker on 12/28/15.
// Copyright © 2015 Slinkworks LLC. All rights reserved.
//
#import "ThingTwo.h"
@implementation ThingTwo
@synthesize stuff;
@synthesize someOtherStuff;
@synthesize someDate;
- (instancetype)init
{
self = [super init];
someDate = [NSDate date];
return self;
}
@end
以下是JSON输出的示例:
{
"values" : [
"Value1",
"Value2",
"Value3"
],
"myInt" : 5431,
"myFloat" : 123.456,
"myBool" : true,
"someMoney" : "$503.00",
"thingTwo" : {
"stuff" : "Thing Two Stuff",
"nullDate" : null,
"someDate" : "1963-01-07 07:10:00 +0000",
"nullString" : null,
"someOtherStuff" : "Thing Two Other Stuff"
},
"name" : "John Jones",
"dict" : {
"key1" : "value1",
"key2" : "value2"
}
}