标签: simplejson

SimpleJson处理相同的命名实体

我在app引擎中使用Alchemy API,所以我使用simplejson库来解析响应.问题是响应的条目具有sme名称

 {
    "status": "OK",
    "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
    "url": "",
    "language": "english",
    "entities": [
        {
            "type": "Person",
            "relevance": "0.33",
            "count": "1",
            "text": "Michael Jordan",
            "disambiguated": {
                "name": "Michael Jordan",
                "subType": "Athlete",
                "subType": "AwardWinner",
                "subType": "BasketballPlayer",
                "subType": "HallOfFameInductee",
                "subType": "OlympicAthlete",
                "subType": "SportsLeagueAwardWinner",
                "subType": "FilmActor",
                "subType": "TVActor",
                "dbpedia": "http://dbpedia.org/resource/Michael_Jordan",
                "freebase": "http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000029161",
                "umbel": "http://umbel.org/umbel/ne/wikipedia/Michael_Jordan",
                "opencyc": "http://sw.opencyc.org/concept/Mx4rvViVq5wpEbGdrcN5Y29ycA",
                "yago": "http://mpii.de/yago/resource/Michael_Jordan"
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

所以问题是"子类型"被重复,因此负载返回的字典只是"TVActor"而不是列表.反正有没有绕过这个?

python json simplejson

3
推荐指数
1
解决办法
1762
查看次数

simplejson.dumps(dict)抛出"not JSON serializable"

我有一个Python字典,其中键是字符串,值是MyObject对象的列表.如果我执行

simplejson.dumps(dict)
Run Code Online (Sandbox Code Playgroud)

它抛出"MyObject not JSON serializable".

如何避免此异常,如何使MyObject可序列化?

python json dictionary simplejson serializable

3
推荐指数
1
解决办法
8638
查看次数

在Python中解析JSON时出现各种错误

试图从需要登录的URL解析json.包括我在这里的所有代码,因为我不确定错误在哪里.

try: import simplejson as json
except ImportError: import json
import urllib2

username = 'user'
password = '1234'
url = "https://www.blah.com/someplace"

# set up the username/password/url request
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, "https://www.blah.com", username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
request = urllib2.Request(url)
response = opener.open(request)

# option 1
json_object = json.loads(str(response))

#option 2
json_object = json.loads(response)
Run Code Online (Sandbox Code Playgroud)

如果我使用选项1运行代码(注释掉选项2),我会收到此错误:

Traceback (most recent call last):
  File "jsontest.py", line 22, in <module>
    json_object = json.loads(str(request))
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 413, in loads
    return …
Run Code Online (Sandbox Code Playgroud)

python json simplejson

3
推荐指数
1
解决办法
2万
查看次数

Simplejson 错误 Python 3.3

我正在尝试运行一个导入 simplejson 的程序。当我在 Python 2.7 中运行它时没问题,但是当我在 Python 3.3 中运行它时,它说:

文件“C:\Python33\lib\simplejson__init__.py”,第 111 行,在 <module> 中
从解码器导入 JSONDecoder,JSONDecodeError
导入错误:没有名为“解码器”的模块

simplejson python-3.x

3
推荐指数
1
解决办法
2867
查看次数

无法使用python解析简单的json

我有一个非常简单的json我无法用simplejson模块解析.再生产:

import simplejson as json
json.loads(r'{"translatedatt1":"Vari\351es"}')
Run Code Online (Sandbox Code Playgroud)

结果:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.5/simplejson/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/pymodules/python2.5/simplejson/decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/pymodules/python2.5/simplejson/decoder.py", line 351, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 1 column 23 (char 23)
Run Code Online (Sandbox Code Playgroud)

任何人都知道什么是错的,以及如何正确解析上面的json?

在那里编码的字符串是:Variées

PS我使用python 2.5

非常感谢!

python json simplejson

3
推荐指数
1
解决办法
6801
查看次数

Django:View返回JSON content_dictionary,如何在Javascript中解码

让我解释一下我正在尝试做什么,如果有人能够指出正确的方法来做到这一点,并解决我陷入困境的问题,那就太棒了!

有人输入网址

www.ABC.com/showItem/Blackberry

我在我的数据库中查找"Blackberry"并找到它的数据,现在我想在一页上显示它的详细信息.

因此在视图中我这样做

return_data=simplejson.dumps(response_dict)

return render_to_response('workmodule/show_item_details.html', {"item_complete_data": return_data}, context_instance=RequestContext(request))

在myHTML中,我这样做

data_from_django = {{ farm_complete_data }}

Question 1:这是访问HTML中的JSON数据的正确方法吗?不知何故,我认为应该有更好/更清洁的方式.

Question 2:另一个问题是所有引号都被替换,"&quot;"因此javscript中断.如果以上是正确的方法,如何正确"解码"字符串.

Note :我之前使用过jquery的.ajax函数,如果你已经在页面上并调用后端,它的效果很好.在这种情况下,视图以与上面相同的方式返回数据,并且数据未被转义.或者说,当我的ajax成功时:或者错误:函数处理它.

感谢您花时间看看这个.

javascript django json simplejson

2
推荐指数
1
解决办法
2847
查看次数

将列表写入文件并使用simplejson将内容读回列表

我想将一个列表写入文件,并将该文件的内容读回列表中.我可以使用simplejson将列表写入文件,如下所示:

f = open("data.txt","w")
l = ["a","b","c"]
simplejson.dump(l,f)
f.close()
Run Code Online (Sandbox Code Playgroud)

现在回来读我的文件

file_contents = simplejson.load(f)
Run Code Online (Sandbox Code Playgroud)

但是,我猜file_contents是json格式.有没有办法将其转换为列表?

谢谢.

python json simplejson

2
推荐指数
1
解决办法
1万
查看次数

简单的json转储函数与unicode

这是使用python2.4版本的简单json示例运行

>>> 
>>> orig='{"key1":"Val", "key2":"val2"}'
>>> origDict = simplejson.loads(orig)
>>> origDict
{'key2': 'val2', 'key1': 'Val'}
>>> origDict['key2'] = '\xe4\xbd\xa0\xe5\xa5\xbd'
>>> simplejson.dumps(origDict)
'{"key2": "\\u4f60\\u597d", "key1": "Val"}'
Run Code Online (Sandbox Code Playgroud)

dumps函数正在用unicode版本替换字节字符串.有没有办法让它不这样做,只返回'{"key2":"\ xe4\xbd\xa0\xe5\xa5\xbd","key1":"Val"}'?

python unicode simplejson

2
推荐指数
1
解决办法
6372
查看次数

在django 1.5 CBV中返回HttpResponse时出现simplejson错误

我对Django很新,尤其是CBV.所以,我有一个带有post方法的listView,我试图输出一个JSON,如下所示:

from django.utils import simplejson

class MyCoolListView(ListView):
   # template declaration and other stuff

    def get_context_data(self, **kwargs):
       # do some stuff
       return context

    def get_queryset(self):
       # do some stuff
       return queryset 

    def post( self, request, *args, **kwargs ):
       # check if user is authenticated and return json
       return HttpResponse( simplejson.dump({ "score": blogpost.score }) , content_type='application/json')
Run Code Online (Sandbox Code Playgroud)

但是,POST上的HttpResponse,我得到:

TypeError: dump() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

我不完全确定我做错了什么(我已经搜索了这个问题,但是还没有运气) - 我想知道是否有人遇到过这样的情况/错误信息.任何解决这个问题的指导都将非常感激.

django json simplejson django-views

2
推荐指数
1
解决办法
1038
查看次数

如何使用 SimpleJson c# 获取 Json 文件中的密钥

我有一个这种格式的json文件:

{
  "3874D632": {
    "FirstName": "Jack",
    "LastName": "Andersen"
  },
  "34F43A33": {
    "FirstName": "John",
    "LastName": "Smith"
  }
Run Code Online (Sandbox Code Playgroud)

}

StreamReader read_json = new StreamReader(path_json_file);

json = JSONNode.Parse(read_json.ReadToEnd());
read_json.Close();
Run Code Online (Sandbox Code Playgroud)

如何获得 3874D632 或 34F43A33 ??

json[????].
Run Code Online (Sandbox Code Playgroud)

c# json simplejson

2
推荐指数
1
解决办法
4134
查看次数