简体   繁体   English

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

[英]How to decode JavaScript response in python 3.4+?

I got this response from a website. 我从一个网站得到了这个回应。 I really don't know how to decode it and get the information that I want. 我真的不知道如何解码它并获取我想要的信息。

Here is the JavaScript response: 这是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"}]}}}}}

I am working with Python 3.4. 我正在使用Python 3.4。 Below is my code 下面是我的代码

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

Thanks in advance! 提前致谢!

—————————————————— ——————————————————

I mistakenly thought it was a JSON file before and asked a bad question. 我之前错误地认为它是JSON文件,并提出了一个错误的问题。 I'm sorry for that. 对此我感到抱歉。 But thank you for helping me! 但是,谢谢您对我的帮助! Hope some of you can help me with my new question! 希望你们中的一些人可以对我的新问题有所帮助!

There are a couple of problems with your input: 您的输入有两个问题:

  1. The braces on your input are unbalanced. 输入上的花括号不平衡。 You're missing the opening brace, { . 您缺少左括号, {
  2. JSON requires that all keys be quoted using double quotes. JSON要求所有键都用双引号引起来。 Your input uses a different syntax, which is valid Javascript but not valid JSON. 您的输入使用不同的语法,该语法是有效的Javascript,但不是有效的JSON。 You can parse such objects into python dictionaries, if you really have to, using a third party package such as demjson . 可以分析这样的对象到Python字典,如果你真的要使用第三方的包,如demjson
>>> import demjson
>>> parsed = demjson.decode('{header:{code:0,message:{title:"",detail:""}}}')
>>> parsed
{u'header': {u'message': {u'detail': '', u'title': ''}, u'code': 0}}

That being said, your original instinct to use json.loads was generally correct. 话虽如此,您使用json.loads本能通常是正确的。 If you have any control over your input whatsoever (eg if it's the output of an API or script that you can re-write,) you should re-write it to output strict JSON , making it parsable by json.loads . 如果您对输入有任何控制(例如,如果可以重写的API或脚本的输出), 则应重写它以输出严格的JSON ,从而使其可被json.loads解析。

It looks like you're data is not strictly JSON because some of the keys are not strings. 看起来您的数据严格来说不是JSON,因为某些键不是字符串。

Also, there seems to be a missing matching curly brace -- should add as the first character, 另外,似乎缺少匹配的花括号-应该添加为第一个字符,

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

Here is a link to w3schools` JSON reference. 是w3schools的JSON参考的链接。

Here is a JSON formatter/validator. 是JSON格式化程序/验证器。

In case you don't want to use third party library, you can just clean up the data by yourself using regex (eg. adding double quote to remaining properties): 如果您不想使用第三方库,则可以使用正则表达式自己清理数据(例如,在其余属性中添加双引号):

import re
import json

text = # the input string

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

It seems you forgot the first curl brace. 看来您忘记了第一个大括号。 The code below can help you parse the data to json: 以下代码可以帮助您将数据解析为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM