Note that there are some explanatory texts on larger screens.

plurals
  1. PONSJSONSerialization - Core Data relationships to JSON
    text
    copied!<p>I've been trying to create a class to allow me to output core data out to JSON.</p> <p>I have managed to get it working to a point, however I seem to have hit a brick wall on a outputting relationships</p> <pre><code>NSMutableArray * objectsArray = [[NSMutableArray alloc] init]; for (NSManagedObject * object in array) { if([NSJSONSerialization isValidJSONObject:object]) { [objectsArray addObject:object]; } else { NSMutableDictionary *fields = [NSMutableDictionary dictionary]; for (NSAttributeDescription *attribute in [[object entity] properties]) { NSString *attributeName = attribute.name; id attributeValue = [object valueForKey:attributeName]; if([results length] &gt; 0) { NSArray *chunks2 = [results componentsSeparatedByString: @","]; for (NSString * string in chunks2) { if([string.lowercaseString isEqualToString:attributeName.lowercaseString]) { [fields setObject:[NSString stringWithFormat:@"%@",attributeValue] forKey:attributeName]; break; } } } else { if (attributeValue) { [fields setObject:[NSString stringWithFormat:@"%@",attributeValue] forKey:attributeName]; } } } [objectsArray addObject:fields]; } } NSError *error; NSData * JSONData = [NSJSONSerialization dataWithJSONObject:objectsArray options:kNilOptions error:&amp;error]; </code></pre> <p>And this outputs data fine aslong as I do not have a relationship for example a one -> many or many -> one</p> <p>It outputs the following</p> <pre><code>{ "mySegmentation": "(null)", "number": "9452062" }, { "mySegmentation": "&lt;NSManagedObject: 0x212050b0&gt; (entity: SegmentationCodes; id: 0x212090b0 &lt;x-coredata://BEC52F5F-EA26-4CFF-BCCB-09DA163F465D/SegmentationCodes/p13&gt; ; data: &lt;fault&gt;)", "number": "9448502" }, </code></pre> <p>How can I get it to also indent in and output the information from the relationship?</p> <p>I have been scratching my head for a while on this and would appreciate the help</p> <p>Thanks Matt</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload