Command-line tool for converting PLIST to JSON?

后端 未结 7 1594
粉色の甜心
粉色の甜心 2020-12-12 10:47

Is there a command line tool available for converting .plist files to JSON?

If not, what would be the approach for creating one using Objective-C or C on a Mac? For

相关标签:
7条回答
  • 2020-12-12 10:53

    There is a native way, to convert plist's to json. It's called NSJSONSerialization.

    Here is an example on how to use it, and convert a plist file to a json file:

    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];
    
    0 讨论(0)
  • 2020-12-12 10:55

    If you are on a Mac you can use the plutil tool on the command line (this comes with the developer tools I believe):

    plutil -convert json Data.plist
    

    as mentioned in the comments, this will overwrite the existing data. To output to a new file

    plutil -convert json -o Data.json Data.plist
    
    0 讨论(0)
  • 2020-12-12 10:59

    I wrote a tool in python to do this. See here:

    http://sourceforge.net/projects/plist2json

    Works from command line on os x or linux distros, batch converts a directory. It's short and simple so it should be easy to modify for your own purposes.

    0 讨论(0)
  • 2020-12-12 11:08

    Using mac utils

    Convert plist to json

    plutil -convert json -o output.json input.plist
    

    Convert json to plist

    plutil -convert xml1 input.json -o output.plist
    
    0 讨论(0)
  • 2020-12-12 11:12

    The following gets the job done—

    // 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_JSON\n", 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);
    }
    

    It does some reasonable error checking, but it's not bullet proof. Use at your own risk.

    You'll need JSONKit to build the tool. Place JSONKit.m and JSONKit.h in the same directory as convertPlistToJSON.m, and then compile with:

    shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
    

    Usage:

    shell% convertPlistTOJSON
    usage: convertPlistToJSON FILE_PLIST FILE_JSON
    
    shell% convertPlistTOJSON input.plist output.json
    

    Reads in input.plist, and writes the pretty printed JSON to output.json.

    0 讨论(0)
  • 2020-12-12 11:13
    plutil -convert json -r -e json Filename.plist
    
    0 讨论(0)
提交回复
热议问题