最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I create json file from plist file? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited May 31, 2012 at 18:13 Kheldar 5,3893 gold badges36 silver badges64 bronze badges asked Jul 29, 2010 at 4:50 sungkwangsongsungkwangsong 5,6554 gold badges27 silver badges18 bronze badges 1
  • 1 I am not sure if any plist could be expressed as a JASON file, what plist exactly you want to convert? – nacho4d Commented Jul 29, 2010 at 5:02
Add a comment  | 

3 Answers 3

Reset to default 12

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.

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

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"}

On OSX you can use the plutil command line tool:

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.

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.

发布评论

评论列表(0)

  1. 暂无评论