如何从plist文件创建json文件?

时间:2021-07-23 02:54:26

I want to create a json file from from existing plist file. How do I create json file from plist file using one of these programming languages: Javascript or Java or Objective-c or Python or Ruby?

我想从现有的plist文件创建一个json文件。如何使用以下编程语言之一从plist文件创建json文件:Javascript或Java或Objective-c或Python或Ruby?

3 个解决方案

#1


11  

Python has a module plistlib which you can use to read plist files. plistlib is available in python >= 2.6, so if you have old version you can get plistlib from here.

Python有一个模块plistlib,您可以使用它来读取plist文件。 plistlib在python> = 2.6中可用,所以如果你有旧版本,你可以从这里获得plistlib。

After reading plist as dict using plistlib.readPlist you can dump it as JSON using json.dumps, for that use json module or for old python version get simplejson

使用plistlib.readPlist读取plist作为dict之后,您可以使用json.dumps将其转储为JSON,为此使用json模块或旧的python版本获取simplejson

Here is an example:

这是一个例子:

plist = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aDict</key>
    <dict>
        <key>anotherString</key>
        <string>&lt;hello hi there!&gt;</string>
    </dict>
    <key>aList</key>
    <array>
        <string>A</string>
        <string>B</string>
        <integer>12</integer>
        <real>32.100000000000001</real>
        <array>
            <integer>1</integer>
            <integer>2</integer>
            <integer>3</integer>
        </array>
    </array>
    <key>aString</key>
    <string>Doodah</string>
</dict>
</plist>
"""

import json
from plistlib import readPlist
import StringIO

in_file = StringIO.StringIO(plist)
plist_dict = readPlist(in_file)

print json.dumps(plist_dict)

Output:

{"aList": ["A", "B", 12, 32.100000000000001, [1, 2, 3]], "aDict": {"anotherString": "<hello hi there!>"}, "aString": "Doodah"}

#2


7  

On OSX you can use the plutil command line tool:

在OSX上,您可以使用plutil命令行工具:

plutil -convert json Data.plist

It isn't using one of those languages, but it saves you from implementing it yourself or messing with libraries.

它没有使用其中一种语言,但它可以避免您自己实现它或乱用库。

#3


3  

Load the property list and get a dictionary out of it. Then using one of the many JSON frameworks, initialize a store-like object (an object that takes a dictionary as input) and write that out to a file. Should create your JSON just fine.

加载属性列表并从中获取字典。然后使用众多JSON框架之一,初始化类似于存储的对象(将字典作为输入的对象)并将其写入文件。应该创建你的JSON就好了。

#1


11  

Python has a module plistlib which you can use to read plist files. plistlib is available in python >= 2.6, so if you have old version you can get plistlib from here.

Python有一个模块plistlib,您可以使用它来读取plist文件。 plistlib在python> = 2.6中可用,所以如果你有旧版本,你可以从这里获得plistlib。

After reading plist as dict using plistlib.readPlist you can dump it as JSON using json.dumps, for that use json module or for old python version get simplejson

使用plistlib.readPlist读取plist作为dict之后,您可以使用json.dumps将其转储为JSON,为此使用json模块或旧的python版本获取simplejson

Here is an example:

这是一个例子:

plist = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aDict</key>
    <dict>
        <key>anotherString</key>
        <string>&lt;hello hi there!&gt;</string>
    </dict>
    <key>aList</key>
    <array>
        <string>A</string>
        <string>B</string>
        <integer>12</integer>
        <real>32.100000000000001</real>
        <array>
            <integer>1</integer>
            <integer>2</integer>
            <integer>3</integer>
        </array>
    </array>
    <key>aString</key>
    <string>Doodah</string>
</dict>
</plist>
"""

import json
from plistlib import readPlist
import StringIO

in_file = StringIO.StringIO(plist)
plist_dict = readPlist(in_file)

print json.dumps(plist_dict)

Output:

{"aList": ["A", "B", 12, 32.100000000000001, [1, 2, 3]], "aDict": {"anotherString": "<hello hi there!>"}, "aString": "Doodah"}

#2


7  

On OSX you can use the plutil command line tool:

在OSX上,您可以使用plutil命令行工具:

plutil -convert json Data.plist

It isn't using one of those languages, but it saves you from implementing it yourself or messing with libraries.

它没有使用其中一种语言,但它可以避免您自己实现它或乱用库。

#3


3  

Load the property list and get a dictionary out of it. Then using one of the many JSON frameworks, initialize a store-like object (an object that takes a dictionary as input) and write that out to a file. Should create your JSON just fine.

加载属性列表并从中获取字典。然后使用众多JSON框架之一,初始化类似于存储的对象(将字典作为输入的对象)并将其写入文件。应该创建你的JSON就好了。