ios - 如何在对象内映射 JSON 数据对象?
<p><p>我正在尝试从 API 调用中获取一些数据并将这些对象映射到模型对象。就我而言,我在一个对象中有一个对象。这是工作并收到 JSON。它与第一个对象的映射很好。在我的 JSON 中,我有一个数组和另一个数组。所以我做了以下事情来完成这项工作。</p>
<p><strong>对象映射</strong></p>
<pre><code>+(NSArray *)fromJSON:(NSDictionary *)objectNotation error:(NSError *__autoreleasing *)error{
NSError *localError = nil;
NSDictionary *parsedObject = [ initWithDictionary:objectNotation];
if (localError != nil) {
*error = localError;
return nil;
}
NSMutableArray *posts = [ init];
NSArray *results = ;
NSLog(@"Count %lu", (unsigned long)results.count);
for (NSDictionary *groupDic in results) {
Post *post = [ init];
post.postBody = ;
for (NSString *key in groupDic) {
if () {
forKey:key];
}
}
;
}
return posts;
}
</code></pre>
<p>由于 for 循环,这个生成的元数据比我预期的要多。</p>
<p><strong>模型类</strong></p>
<pre><code>@interface OCPost : NSObject
@property(nonatomic) NSInteger postId;
@property(nonatomic) NSInteger userId;
@property(nonatomic) NSInteger points;
@property(strong, nonatomic) NSMutableArray *body;
@end
</code></pre>
<p>我想将 <code>body</code> 数组映射到 <code>Body</code> 对象数组。在我的 body 课上</p>
<pre><code>@interface Body : NSObject
@property (strong, nonatomic) NSString *value;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSMutableArray *metaData;
@end
</code></pre>
<p><code>元数据</code>也应该与以下对象映射。</p>
<pre><code>@interface MetaData : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *metaDataDescription;
@end
</code></pre>
<p>我怎样才能制作一个像,</p>
<pre><code>Post
|_Body1
||_MetaData1
||_MetaData2
||_MetaData3
|_Body3
||_MetaData1
||_MetaData2
</code></pre>
<p>请帮忙。</p>
<p><strong>JSON</strong></p>
<pre><code>{
"message": "Post ",
"data": [
{
"postId": 1,
"timestamp": "2016-06-14 22:37:02",
"body": [
{
"textId": 1,
"type": "link",
"deleting": false,
"metaData": [
{
"metadataId": 1,
"type": "text",
"value": "under "
}
]
},
{
"textId": 2,
"type": "department",
"deleting": false,
"metaData": [
{
"metadataId": 2,
"type": "text",
"value": "under "
}
]
}
]
}
]
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,您可以手动解析 JSON 并将其映射到您的数据模型。<br/>
这是你可以做到的:</p>
<pre><code>+(NSArray *)fromJSON:(NSDictionary *)objectNotation error:(NSError *__autoreleasing *)error{
NSError *localError = nil;
NSDictionary *parsedObject = [ initWithDictionary: objectNotation];
if (localError != nil) {
*error = localError;
return nil;
}
NSMutableArray *posts = [ init];
NSArray *results = ;
NSLog(@"Count %lu", (unsigned long)results.count);
for (NSDictionary *groupDic in results) {
OCPost *post = [ init];
post.body = ;
//1. Parse bodies
NSArray *bodies = groupDic[@"body"];
for (NSDictionary *aBody in bodies) {
Body *body = [ init];
body.value = aBody[@"textId"];
body.name = aBody[@"type"];
body.metaData = [@[] mutableCopy];
//2. Parse metadatas
NSArray *metadatasOfBody = aBody[@"metaData"];
for (NSDictionary *aMetadata in metadatasOfBody) {
MetaData *metadata = [ init];
metadata.title = aMetadata[@"type"];
metadata.metaDataDescription = aMetadata[@"value"];
//3. Add metadata to body
;
}
//4. Add body to post
;
}
//Logs to check post body and body metadata
NSLog(@"post %@", post);
NSLog(@"post body %@", post.body);
for (Body *body in post.body) {
NSLog(@"post body metadata %@", body.metaData);
}
//5. Add post to posts
;
}
//Log to check return posts
NSLog(@"posts %@", posts);
return posts;
}
</code></pre>
<p>要对其进行测试,您可以将 JSON 表示为 NSDictionary,如下所示:</p>
<pre><code>- (NSDictionary*) aJSON
{
return @{
@"message": @"Post ",
@"data": @[
@{
@"postId": @1,
@"timestamp": @"2016-06-14 22:37:02",
@"body": @[
@{
@"textId": @1,
@"type": @"link",
@"deleting": @false,
@"metaData": @[
@{
@"metadataId": @1,
@"type": @"text",
@"value": @"under "
}
]
},
@{
@"textId": @2,
@"type": @"department",
@"deleting": @false,
@"metaData": @[
@{
@"metadataId": @2,
@"type": @"text",
@"value": @"under "
}
]
}
]
}
]
};
}
</code></pre>
<p>将 <code>objectNotation</code> 替换为 <code></code> 以便能够在不调用服务器 API 的情况下测试映射,当然也可以调用服务器 API 端点,并尝试使用响应进行测试JSON。</p></p>
<p style="font-size: 20px;">关于ios - 如何在对象内映射 JSON 数据对象?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37846858/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37846858/
</a>
</p>
页:
[1]