从plist文件创建json文件可以通过以下几种方法:
import plistlib
import json
# 读取plist文件
with open('example.plist', 'rb') as f:
plist_data = plistlib.load(f)
# 将plist数据转换为json
json_data = json.dumps(plist_data, ensure_ascii=False, indent=4)
# 将json数据写入文件
with open('example.json', 'w') as f:
f.write(json_data)
import Foundation
// 读取plist文件
if let path = Bundle.main.path(forResource: "example", ofType: "plist"),
let dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
// 将plist数据转换为json
if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
// 将json数据写入文件
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = url.appendingPathComponent("example.json")
try? jsonData.write(to: fileURL)
}
}
plutil -convert json example.plist -o - > example.json
可以使用在线的plist和json转换工具,例如:https://www.freeformatter.com/plist-to-json-converter.html
可以使用第三方库,例如:https://github.com/mattt/PlistKit
这些方法都可以将plist文件转换为json文件。在选择方法时,请根据您的需求和技能水平进行选择。
领取专属 10元无门槛券
手把手带您无忧上云