PLIST를 JSON으로 변환하기 위한 명령줄 도구?
.plist 파일을 JSON으로 변환하기 위한 명령줄 도구가 있습니까?
그렇지 않은 경우 Mac에서 Objective-C 또는 C를 사용하여 생성하는 방법은 무엇입니까?예를 들어 Objective-C에는 JSONKit이 있습니다..plist 파일을 열어 JSONKit에 전달하고 JSON으로 시리얼화하려면 어떻게 해야 할까요?
Mac을 사용하는 경우 명령줄에서 flutil 도구를 사용할 수 있습니다(이것은 개발자 도구와 함께 제공됩니다).
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_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);
}
합리적인 오류 확인은 하지만 방탄은 아닙니다.본인 부담으로 사용하세요.
툴을 구축하려면 JSONKit이 필요합니다.장소JSONKit.m
그리고.JSONKit.h
와 같은 디렉토리에convertPlistToJSON.m
다음으로 컴파일합니다.
shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
사용방법:
shell% convertPlistTOJSON
usage: convertPlistToJSON FILE_PLIST FILE_JSON
shell% convertPlistTOJSON input.plist output.json
읽음input.plist
인쇄된 예쁜 JSON을 에 씁니다.output.json
.
mac 유틸리티 사용
plist를 json으로 변환
plutil -convert json -o output.json input.plist
json을 plist로 변환
plutil -convert xml1 input.json -o output.plist
이 방법은 매우 간단합니다.
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];
저는 3개의 파일만 작성하면 되기 때문에 인수를 받아 들일 기회가 없었습니다.
이걸 하기 위해 파이썬으로 도구를 썼어요.여기를 참조해 주세요.
http://sourceforge.net/projects/plist2json
os x 또는 Linux distros 명령줄에서 작동하며, 일괄적으로 디렉토리를 변환합니다.짧고 간단하기 때문에 자신의 목적에 맞게 쉽게 수정할 수 있을 것입니다.
원어민적인 방법이 있다.plist
~에 대해서json
NSJONSerialization이라고 합니다.
다음으로 사용방법 및 변환방법 예를 제시하겠습니다.plist
로 줄지어 가다.json
파일:
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];
변환Filename.plist
로.Filename.json
:
plutil -convert json -r -e json Filename.plist
-convert
형식을 나타냅니다.-r
출력을 보다 인간적으로 만듭니다.-e
내선번호를 지정합니다.
"목적지 형식의 plist에서 잘못된 개체"와 관련된 문제가 발생할 경우 plist에 바이트와 같은 데이터가 있을 수 있습니다. 이 데이터는 json에 대해 직렬화할 수 있는 것으로 간주되지 않습니다.
Python에는 기본 제공 plist 지원이 있으며, 직렬화가 실패했을 때 수행할 작업(예: 바이트)을 지정하는 사용자 정의 기본 함수를 지정할 수 있습니다.bytes 필드에 관심이 없는 경우 다음과 같이 일련화할 수 있습니다.'<not serializable>'
다음 python 1-liner를 사용합니다.
python -c 'import plistlib,sys,json; print(json.dumps(plistlib.loads(sys.stdin.read().encode("utf-8")), default=lambda o:"<not serializable>"))'
이것은 stdin을 취해서 prist를 해석한 다음 json으로 덤프합니다.예를 들어 Mac의 현재 전원 정보를 가져오려면 다음을 실행합니다.
ioreg -rw0 -c AppleSmartBattery -a | python -c 'import plistlib,sys,json; print(json.dumps(plistlib.loads(sys.stdin.read().encode("utf-8")), default=lambda o:"<not serializable>"))'
는, 로 접속할 수 .jq '.[0].AdapterDetails.Watts'
언급URL : https://stackoverflow.com/questions/6066350/command-line-tool-for-converting-plist-to-json
'it-source' 카테고리의 다른 글
컴포넌트에 대한 리액트라우터 패스 파라미터 (0) | 2023.03.22 |
---|---|
미정의 In React 확인 (0) | 2023.03.22 |
bigint: 12000000000002539의 JSON 전송은 12000000000002540으로 변환됩니까? (0) | 2023.03.22 |
WordPress를 사용하여 URL 개서를 위해 Windows Azure를 설정하는 방법 (0) | 2023.03.22 |
워드프레스:고급 사용자 지정 필드: 새 워드프레스 설치로 필드 내보내기 및 가져오기 (0) | 2023.03.22 |