[英]parsing json using JSON.parse() throws error
我有一個很大的json字符串,該字符串是從encodeURIComponent()函數解碼的。 現在,我嘗試使用JSON.parse()函數將此字符串解析為Json對象。 當我運行時,它說“意外的語法標記m”。 我在這里有兩個疑問:1.是否有更好的工具來校正語法,因為它是一個很大的字符串。 2.當我在該URL“ http://jsonviewer.stack.hu ”中提供相同的json字符串進行驗證時,我將獲得有效的json對象。這怎么可能。
我要進入第2點的有效json對象就是我在程序中需要的對象。 請給我一些有關如何解決此問題的建議。
這是我的json字符串: https : //drive.google.com/file/d/0Bz0478Hh7tBqS0pXUTBMTVFPZzA/view? usp =sharing
我個人使用npm包jsonlint
來驗證大型JSON文件。
npm install -g jsonlint
用法很簡單,它將准確告訴您JSON違例的存在位置。
Usage: jsonlint [file] [options]
file file to parse; otherwise uses stdin
Options:
-v, --version print version and exit
-s, --sort-keys sort object keys
-i, --in-place overwrite the file
-t CHAR, --indent CHAR character(s) to use for indentation [ ]
-c, --compact compact error display
-V, --validate a JSON schema to use for validation
-e, --environment which specification of JSON Schema the validation
file uses [json-schema-draft-03]
-q, --quiet do not print the parsed json to STDOUT [false]
-p, --pretty-print force pretty printing even if invalid
它抱怨的“ m”是第一個字符。 您需要在開頭刪除mandrill_events=
部分,以使其成為有效的JSON。
mandrill_events=
開頭就是打破JSON.parse(...)
。 刪除它。
回答您有關該工具的問題,我很喜歡這一工具。
http://www.jsoneditoronline.org/
只需粘貼您的JSON,然后按向右箭頭即可。 我在兩個方向上都可以工作,您也可以在右側編輯JSON。
如果您的JSON對象有問題,它將告訴您確切的位置。 它在后台使用jsonlint。
在您的情況下,問題可能是其他答案所說的初始變量賦值,因為這不是有效的JSON。 您需要刪除它,只保留數組。 JSON在線編輯器會拋出此錯誤,它的描述性更高。
Error: Parse error on line 1:
mandrill_events=[{"e
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.