如何在python 3.4+中解码JavaScript响应?

时间:2017-01-17 04:30:03

标签: javascript python

我从网站得到了这个回复。我真的不知道如何解码它并获取我想要的信息。

以下是JavaScript响应:

{header:{code:0,message:{title:"",detail:""}},body:{parameters:{},dataStores:{"kccjStore":{pageSize:100,pageNumber:1,recordCount:3,name:"kccjStore",rowSetName:"pojo_com.neusoft.education.sysu.xscj.xscjcx.model.KccjModel",order:"kclb asc",metaData:{"jxbmc":{dataType:12},"njzypm":{dataType:12},"xh":{dataType:12},"jxbh":{dataType:12},"sftg":{dataType:12},"zzcj":{dataType:12},"cjzt":{dataType:12},"bzw":{dataType:12},"class":{dataType:12},"xs":{dataType:12},"xq":{dataType:12},"jsxm":{dataType:12},"xnd":{dataType:12},"resource_id":{dataType:12},"jxbpm":{dataType:12},"jd":{dataType:12},"kch":{dataType:12},"kcywmc":{dataType:12},"kcmc":{dataType:12},"khfs":{dataType:12},"cjlcId":{dataType:12},"xf":{dataType:12},"kclb":{dataType:12},"zpcj":{dataType:12}},rowSet:{primary:[{"jxbpm":"79\/93","bzw":"00","cjzt":"2","cjlcId":"11895237815","kcywmc":"Mobile Information Engineering Training:Intermediate ","jxbh":"46000092161002","zpcj":"90","resource_id":"11895237815","xnd":"2016-2017","xq":"1","kch":"46000092","kcmc":"移动信息工程实训(中级)","kclb":"11","xf":"3.0","xs":"72.0","zzcj":"90","jd":"4.0","sftg":"1","xh":"14353379","njzypm":"304\/403","class":"class com.neusoft.education.sysu.xscj.xscjcx.model.KccjModel"},{"jxbpm":"74\/128","bzw":"00","cjzt":"2","cjlcId":"12825792491","kcywmc":"Database Systems Lab","jxbh":"46000085161003","jsxm":"桑应朋","khfs":"03","zpcj":"94","resource_id":"12825792491","xnd":"2016-2017","xq":"1","kch":"46000085","kcmc":"数据库系统实验","kclb":"21","xf":"1.0","xs":"36.0","zzcj":"94","jd":"4.4","sftg":"1","xh":"14353379","njzypm":"157\/403","class":"class com.neusoft.education.sysu.xscj.xscjcx.model.KccjModel"},{"jxbpm":"20\/167","bzw":"00","cjzt":"2","cjlcId":"12825622928","kcywmc":"Database Systems","jxbh":"46000084161001","jsxm":"桑应朋","khfs":"01","zpcj":"87","resource_id":"12825622928","xnd":"2016-2017","xq":"1","kch":"46000084","kcmc":"数据库系统","kclb":"21","xf":"2.0","xs":"36.0","zzcj":"87","jd":"3.7","sftg":"1","xh":"14353379","njzypm":"34\/403","class":"class com.neusoft.education.sysu.xscj.xscjcx.model.KccjModel"}]}}}}}

我正在使用Python 3.4。以下是我的代码

html_doc = opener.open('http://wjw.sysu.edu.cn/api/score?year=2016-2017&term=1&pylb=01').read().decode('utf-8')

提前致谢!

------------------

我之前错误地认为它是一个JSON文件并且问了一个糟糕的问题。对不起,我很抱歉。但是谢谢你的帮助!希望你们中的一些人可以帮我解决我的新问题!

4 个答案:

答案 0 :(得分:1)

您的输入存在一些问题:

  1. 您输入的大括号不平衡。你错过了左大括号{
  2. JSON要求使用双引号引用所有键。您的输入使用不同的语法,这是有效的Javascript但不是有效的JSON。如果你真的需要,可以将这些对象解析为python词典,使用第三方包,例如demjson
  3. >>> import demjson
    >>> parsed = demjson.decode('{header:{code:0,message:{title:"",detail:""}}}')
    >>> parsed
    {u'header': {u'message': {u'detail': '', u'title': ''}, u'code': 0}}
    

    话虽如此,您使用json.loads的原始本能通常是正确的。如果您对输入有任何控制(例如,如果它是您可以重写的API或脚本的输出),那么应该将其重新写入输出严格的JSON ,使其可以解析为json.loads

答案 1 :(得分:0)

看起来你的数据不是严格的JSON,因为有些键不是字符串。

此外,似乎缺少匹配的大括号 - 应该添加为第一个字符

person.address.push(person.address.join(', '))

Here是w3schools` JSON参考的链接。

Here是JSON格式化程序/验证程序。

答案 2 :(得分:0)

如果您不想使用第三方库,您可以使用正则表达式自行清理数据(例如,为剩余属性添加双引号):

import re
import json

text = # the input string

p = re.compile(r'({|,)([^{}:"]+):')
text = p.sub(r'\1"\2":', '{' + text)
obj = json.loads(text)

答案 3 :(得分:0)

好像你忘记了第一个卷发支架。下面的代码可以帮助您将数据解析为json:

s = <YOUR DATA>
import json
import re

def repl(m):
    return '"%s":'%m.group(1)

s = re.sub(r'(\w+):', repl, s)
o = json.loads(s)
print o