Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- plutil -convert json Data.plist
- plutil -convert json -o Data.json Data.plist
- // convertPlistToJSON.m
- #import <Foundation/Foundation.h>
- #import "JSONKit.h"
- int main(int argc, char *argv[]) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- if(argc != 3) { fprintf(stderr, "usage: %s FILE_PLIST FILE_JSONn", argv[0]); exit(5); }
- NSString *plistFileNameString = [NSString stringWithUTF8String:argv[1]];
- NSString *jsonFileNameString = [NSString stringWithUTF8String:argv[2]];
- NSError *error = NULL;
- NSData *plistFileData = [NSData dataWithContentsOfFile:plistFileNameString options:0UL error:&error];
- if(plistFileData == NULL) {
- NSLog(@"Unable to read plist file. Error: %@, info: %@", error, [error userInfo]);
- exit(1);
- }
- id plist = [NSPropertyListSerialization propertyListWithData:plistFileData options:NSPropertyListImmutable format:NULL error:&error];
- if(plist == NULL) {
- NSLog(@"Unable to deserialize property list. Error: %@, info: %@", error, [error userInfo]);
- exit(1);
- }
- NSData *jsonData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error];
- if(jsonData == NULL) {
- NSLog(@"Unable to serialize plist to JSON. Error: %@, info: %@", error, [error userInfo]);
- exit(1);
- }
- if([jsonData writeToFile:jsonFileNameString options:NSDataWritingAtomic error:&error] == NO) {
- NSLog(@"Unable to write JSON to file. Error: %@, info: %@", error, [error userInfo]);
- exit(1);
- }
- [pool release]; pool = NULL;
- return(0);
- }
- shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
- shell% convertPlistTOJSON
- usage: convertPlistToJSON FILE_PLIST FILE_JSON
- shell% convertPlistTOJSON input.plist output.json
- NSArray* array = [[NSArray arrayWithContentsOfFile:[@"~/input.plist" stringByExpandingTildeInPath]]retain];
- SBJsonWriter* writer = [[SBJsonWriter alloc] init];
- NSString* s = [[writer stringWithObject:array] retain];
- [s writeToFile:[@"~/output.json" stringByExpandingTildeInPath] atomically:YES];
- [array release];
- NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:@"input.plist"];
- NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict options:NSJSONWritingPrettyPrinted error:&error];
- NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- [jsonString writeToFile:@"output.json" atomically:NO encoding:NSUTF8StringEncoding error:&error];
- plutil -convert json -o output.json input.plist
- plutil -convert xml1 input.json -o output.plist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement