Python将字符串读入json

时间:2016-07-19 18:57:55

标签: python json

我有这个字符串

"{u'Status': u'Up About an hour', u'Created': 1468874455, u'Image': u'instavote/vote', u'Labels': {u'com.docker.compose.service': u'webapp', u'com.docker.compose.config-hash': u'01e63b02746f28876c67969a7dfb39cd68ee598b8edc062a00a2812114c660a1', u'com.docker.compose.project': u'vote', u'com.docker.compose.version': u'1.6.2', u'com.docker.compose.oneoff': u'False', u'com.docker.compose.container-number': u'2'}, u'NetworkSettings': {u'Networks': {u'vote_default': {u'NetworkID': u'', u'MacAddress': u'02:42:ac:13:00:02', u'GlobalIPv6PrefixLen': 0, u'Links': None, u'GlobalIPv6Address': u'', u'IPv6Gateway': u'', u'IPAMConfig': None, u'EndpointID': u'6aeb163ac91970e9d5d62edc99f8ee27e5ac696fb0f662859ee9da097dcf4df5', u'IPPrefixLen': 16, u'IPAddress': u'172.19.0.2', u'Gateway': u'172.19.0.1', u'Aliases': None}}}, u'HostConfig': {u'NetworkMode': u'vote_default'}, u'ImageID': u'sha256:cfcbf877123035ee3c458842c72d1d2204043fad686e9433353c69ed5ed762e5', u'State': u'running', u'Command': u'gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0', u'Names': [u'/vote_webapp_2'], u'Mounts': [], u'Id': u'5341fc7297ce047bda8d55db7b61b0265c97d050ac6eb163a084b28301de1a2f', u'Ports': [{u'Type': u'tcp', u'PrivatePort': 80}]}"
"{u'Status': u'Up About an hour', u'Created': 1468874455, u'Image': u'instavote/vote', u'Labels': {u'com.docker.compose.service': u'webapp', u'com.docker.compose.config-hash': u'01e63b02746f28876c67969a7dfb39cd68ee598b8edc062a00a2812114c660a1', u'com.docker.compose.project': u'vote', u'com.docker.compose.version': u'1.6.2', u'com.docker.compose.oneoff': u'False', u'com.docker.compose.container-number': u'2'}, u'NetworkSettings': {u'Networks': {u'vote_default': {u'NetworkID': u'', u'MacAddress': u'02:42:ac:13:00:02', u'GlobalIPv6PrefixLen': 0, u'Links': None, u'GlobalIPv6Address': u'', u'IPv6Gateway': u'', u'IPAMConfig': None, u'EndpointID': u'6aeb163ac91970e9d5d62edc99f8ee27e5ac696fb0f662859ee9da097dcf4df5', u'IPPrefixLen': 16, u'IPAddress': u'172.19.0.2', u'Gateway': u'172.19.0.1', u'Aliases': None}}}, u'HostConfig': {u'NetworkMode': u'vote_default'}, u'ImageID': u'sha256:cfcbf877123035ee3c458842c72d1d2204043fad686e9433353c69ed5ed762e5', u'State': u'running', u'Command': u'gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0', u'Names': [u'/vote_webapp_2'], u'Mounts': [], u'Id': u'5341fc7297ce047bda8d55db7b61b0265c97d050ac6eb163a084b28301de1a2f', u'Ports': [{u'Type': u'tcp', u'PrivatePort': 80}]}"

我想用以下内容读入json对象:

json.loads(json.dumps(c))

而不是删除第一个"和最后一个",然后通过'更改"是否有更快的方法将此字符串转换为json对象?

1 个答案:

答案 0 :(得分:1)

您可以使用ast.literal_eval()

<workflow-app name="Spark_on_Oozie" xmlns="uri:oozie:workflow:0.5">
  <global>
            <configuration>
                <property>
                    <name>oozie.launcher.yarn.app.mapreduce.am.env</name>
                    <value>SPARK_HOME=/usr/lib/spark</value>
                </property>
            </configuration>
  </global>
    <start to="spark-9fa1"/>
    <kill name="Kill">
        <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <action name="spark-9fa1">
        <spark xmlns="uri:oozie:spark-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <master>yarn-cluster</master>
            <mode>client</mode>
            <name>spak_job</name>
             <class>clear</class>
            <jar>/home/cloudera/DQ_FRAMEWORK/oozie/application/app_amlmkte_dq/wf_prc_l1_dq/dq_prc_ini_com_spark.py</jar>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>
    <end name="End"/>
</workflow-app>


Pyspark Job (Only Put the small code to test):

if __name__ == "__main__":

    sc = SparkContext(appName="Aml Markets DQ")
    sqlContext=HiveContext(sc)
    dt1=datetime.datetime.now()
    dq_exec_start_tm=('%02d%02d%02d%02d%02d%02d%d'%(dt1.year,dt1.month,dt1.day,dt1.hour,dt1.minute,dt1.second,dt1.microsecond))[:-4]
    #dq_batch_start_id=app_nm +'_'+('%02d%02d%02d%02d%02d%02d%d'%(dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond))[:-4]
    # Command Line Arguement