2013-10-29 1 views
1

그룹 프로젝트의 경우 Python으로 JSON 객체를 가져와야합니다. 나쁜 점은 JSON 문자열이 올바른지 (JSONLint를 사용하여 선택) JSON 문자열이 맞지만 Python이 오류 메시지를 계속 표시한다는 것입니다. 이 스크립트는 나는 다음과 같이이 테스트하는 데 사용할 :Python JSON 모듈은 입력이 정확하더라도 오류를 표시합니다.

바로 그러한 큰 JSON 구조로 도움을 요청하지만이 코드 조각이 이제 한 시간 이상 내 기어를 분쇄되었고, 난 그냥 수에 대한
import json 

correct_json = """ { 
    "created_at": "Tue Feb 19 18:42:07 +0000 2013", 
    "id": 303937526471725060, 
    "id_str": "303937526471725056", 
    "text": "Batavierenrace door #Ulft: Eind april klinkt jaarlijks het startschot voor de grootste estafetteloop v... http:\\/\\/t.co\\/hijGD3pk #Enschede", 
    "source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\"> twitterfeed </a>", 
    "truncated": false, 
    "in_reply_to_status_id": null, 
    "in_reply_to_status_id_str": null, 
    "in_reply_to_user_id": null, 
    "in_reply_to_user_id_str": null, 
    "in_reply_to_screen_name": null, 
    "user": { 
     "id": 258430204, 
     "id_str": "258430204", 
     "name": "EnschedeNieuws", 
     "screen_name": "nieuws_enschede", 
     "location": "", 
     "url": "http://drimble.nl/regio/overijssel/enschede/", 
     "description": "AlhetnieuwsoverEnschede", 
     "protected": false, 
     "followers_count": 1344, 
     "friends_count": 17, 
     "listed_count": 18, 
     "created_at": "Sun Feb 27 18:17:21 +0000 2011", 
     "favourites_count": 0, 
     "utc_offset": null, 
     "time_zone": null, 
     "geo_enabled": false, 
     "verified": false, 
     "statuses_count": 20044, 
     "lang": "en", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "profile_background_color": "131516", 
     "profile_background_image_url": "http://a0.twimg.com/images/themes/theme14/bg.gif", 
     "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif", 
     "profile_background_tile": true, 
     "profile_image_url": "http://a0.twimg.com/profile_images/1256839194/enschede_normal.jpg", 
     "profile_image_url_https": "https://si0.twimg.com/profile_images/1256839194/enschede_normal.jpg", 
     "profile_link_color": "009999", 
     "profile_sidebar_border_color": "EEEEEE", 
     "profile_sidebar_fill_color": "EFEFEF", 
     "profile_text_color": "333333", 
     "profile_use_background_image": true, 
     "default_profile": false, 
     "default_profile_image": false, 
     "following": null, 
     "follow_request_sent": null, 
     "notifications": null 
    }, 
    "geo": null, 
    "coordinates": null, 
    "place": null, 
    "contributors": null, 
    "retweet_count": 0, 
    "entities": { 
     "hashtags": [ 
      { 
       "text": "Ulft", 
       "indices": [ 
        20, 
        25 
       ] 
      }, 
      { 
       "text": "Enschede", 
       "indices": [ 
        127, 
        136 
       ] 
      } 
     ], 
     "urls": [ 
      { 
       "url": "http://t.co/hijGD3pk", 
       "expanded_url": "http://bit.ly/UE0MCq", 
       "display_url": "bit.ly/UE0MCq", 
       "indices": [ 
        106, 
        126 
       ] 
      } 
     ], 
     "user_mentions": [] 
    }, 
    "favorited": false, 
    "retweeted": false, 
    "possibly_sensitive": false 
} """ 

other_json = """ { \ 
    "foo" : 5,\ 
    "bar" : ["spam", "eggs"] \ 
} """ 

print(json.dumps(json.loads(correct_json), sort_keys=True, indent=4 * ' ')) 

죄송합니다 오류를 발견하지 못합니다. 훈련 된 JSON 형사가 나를 도울 수 있기를 바랍니다. 오류 파이썬 날입니다 제공 :

D:\Documenten\Dropbox>python jsontest.py 
Traceback (most recent call last): 
    File "jsontest.py", line 99, in <module> 
    print(json.dumps(json.loads(correct_json), sort_keys=True, indent=4 * ' ')) 
    File "C:\Python33\lib\json\__init__.py", line 319, in loads 
    return _default_decoder.decode(s) 
    File "C:\Python33\lib\json\decoder.py", line 352, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode 
    obj, end = self.scan_once(s, idx) 
ValueError: Expecting ',' delimiter: line 6 column 25 (char 305) 

내가 파이썬 아마 바보 뭔가를 누락 3.를 사용하고 있습니다 (이것은 처음되지 않을 것). 미리 감사드립니다!

+0

왜 "" "로 시작합니까?" – MONTYHS

+0

@MONTYHS : 멀티 라인 문자열을 사용하지 않고 멀티 라인 문자열을 사용하려면 (멀티 스트럭쳐가 가능한 것은 확실하지 않음) 및 리터럴 문자열 해석 (나는 맞춰야한다) – bobismijnnaam

답변

2

그것의이 라인에 이중 이스케이프 문제 : 당신이 탈출 상태를 유지하기 위해 필요로 할 때이 \"에서

"source": "<a href=\"http://twitterfeed.com\" rel=\"nofollow\"> twitterfeed </a>", 

"으로 해석지고 있습니다. 따라서 esc37 문자를 벗어나야합니다 (예 : \\"). 그러면 \"이됩니다.

"source": "<a href=\\"http://twitterfeed.com\\" rel=\\"nofollow\\"> twitterfeed </a>", 

또는, 그래서 같은 원시 문자열로 전체 문자열을 선언 :

그래서 수정은

correct_json = r""" { 

그러나이 의지 엉망이 이미 일부 시퀀스를 탈출했다.

+0

고마워 이제 마침내 내 프로젝트를 계속 진행할 수 있습니다! – bobismijnnaam