如何将invaild json变成vaild json?

时间:2018-01-28 17:40:38

标签: jquery json

我试图在jQuery中读取一个有300个产品的json文件,但它不起作用,因为它不是vaild json。对于文件中的每个产品,它目前看起来都是这样的。这只是2个产品的一个例子。

{ "products": [
{"asin": "B0002FHIWQ", "title": "Hanes Unisex Beefy-T 100% Cotton Long 
Sleeve T-Shirt", "imUrl": "http://ecx.images-
amazon.com/images/I/41tQBha0m3L._SY445_.jpg", "related": 
{"also_bought": ["B0002FHIMQ", "B0002FHIM6", "B000M9N0LA"], 
"also_viewed": ["B00147ZMWY", "B0002FHIMQ", "B000VSHUNG", "B003EZHGYQ", 
"B003EZJHOI", "B006WOD4DS", "B0049A9FVW", "B001MB5KS8", "B000PFL6Y4", 
"B00BBQG09M", "B0006TSYZI", "B003AU5WJQ", "B0002X4HTK", "B000BUVLIE", 
"B006ZS8Y54", "B000ID194A", "B004BH0GRK", "B0012N37TA", "B00KUL3NOO", 
"B00KUL34WU", "B00BBWBNFC", "B001B2JIMC", "B0071NMHDC", "B004ZBM5KS", 
"B00BDBEQJ6", "B003IXBEX8", "B003UWAIT8", "B00H32OK86", "B000KBCQ74", 
"B0012N84G6", "B003TPBRBY", "B002UZ3S9S", "B001KL7GJG", "B003UWF6P4", 
"B008ER1JP6", "B006ZS9C1O", "B004ZBM5OY", "B0013EIEE6", "B003UWC6AC", 
"B005Z1TOD8", "B006Z8FRAO", "B004R1ENGU", "B00EOZGC7W", "B002YRV3W6", 
"B007C39WIE", "B00H32OEQE", "B000CBV8PS", "B0012N84DY", "B00266PCMW", 
"B00AOO14HA", "B00BF3WYQE", "B00BBQ9PGM", "B002G9U8VE", "B0002VNLC6", 
"B00AYYXW3E", "B0002FHIVM"], "bought_together": ["B00147ZMWY", 
"B0049A9FVW", "B000VSHUNG"]}, "salesRank": {"Clothing": 6992}, 
"categories": [["Sports & Outdoors", "Clothing", "Men", "Shirts", "T-
Shirts"], ["Clothing, Shoes & Jewelry", "Men", "Clothing", "Shirts", 
"T-Shirts"], ["Clothing, Shoes & Jewelry", "Men", "Big & Tall"]]}
{"asin": "B0002FHIM6", "title": "Fruit of the Loom 5.4 oz.Cotton T-
Shirt", "imUrl": "http://ecx.images-
amazon.com/images/I/41oKghMoCML._SY445_.jpg", "related": 
{"also_bought": ["B0002FHIMQ", "B000KK2O5Y"], "also_viewed": 
["B0007IBYO6", "B0050AFWTO", "B0002FHIVM", "B00079WDZ4", "B00B81I4IU", 
"B0002FHIXK", "B000BTIKQQ", "B00DOFXPSM", "B00B81I4HQ", "B00AZ70UBW", 
"B000P50YLK", "B000BVGLTM", "B003YQLC6I", "B0013EIS88", "B00AZ6Z1DA", 
"B003IX3EWC", "B00ACIFB5O", "B000BY41FK", "B000853HJI", "B000P57EUE", 
"B000ICX36I", "B0010EG85Q", "B00CIZH8X2", "B00AYYXW3E", "B00HZ31A2C", 
"B0002FHILW", "B00LP8LUIW", "B0002FHINA", "B00CMP7R02", "B0071NMH9G", 
"B00BF400TG", "B0012NBP58", "B00H32BZLQ", "B001RTSCUI", "B00CMP60J6", 
"B001T3UBVA", "B000CBV8PS", "B00BEO1Y1U", "B0014JFFZ6", "B00G6N8ONU", 
"B0012N37OA", "B00A7QU8ZY", "B0014C5490", "B00CMP5RTK", "B0049A9FVW", 
"B005LLP0LM", "B000BWI0AO", "B00ACIF9WE", "B005CW054C", "B0002FHIMQ", 
"B003TP8HJ4", "B0012F5U3E"], "bought_together": ["B0002FHIVM", 
"B00079WDZ4"]}, "salesRank": {"Clothing": 31}, "categories": [["Sports 
& Outdoors", "Clothing"], ["Clothing, Shoes & Jewelry", "Men", 
"Clothing", "Shirts", "T-Shirts"]]}
]}

哪个不是vaild json我已经让它看起来第一个产品,但文件只有300多个产品,所以必须更容易手动完成所有这些?

{ "products": [
    {
 "asin": "B0001MQ60A",
 "title": "KEEN Men's Newport H2 Sandal",
 "imUrl": "http://ecx.images-amazon.com/images/I/41pRaO7fFSL._SX395_.jpg",
 "related": {"also_bought": ["B000MN8OR6"],
   "also_viewed": ["B0000DYKET", "B0035FE60M", "B008KI85R4", "B000MQWVA4", "B003Z4KHXS", "B00GUBOCGQ",
     "B003O2SLXY", "B0017KSRMA", "B003O2SBKM", "B00DSN637U", "B000HDJ8IK", "B00E0J3HVG", "B003Z4KGZW",
     "B005HO2CYG", "B003H4QFVY", "B0017LB2VC", "B002R8JPTK", "B008KI84SE", "B005DJDL9A", "B003TU14OE",
     "B00E0J3HTI", "B000EDTVYY", "B003Z4JOJG", "B00DSN638E", "B00E0J3HVQ", "B008KI88JY", "B00EZIRE20",
     "B0095RGEH2", "B00CEX6MSU", "B000B84URK", "B003O2SPMG", "B002KKCWP4", "B003O2SLXE", "B00JQHFV0M",
     "B008JE8V14", "B0055ATVDW", "B003Z4KLMA", "B008ZAY40Y", "B003H4QFV4", "B00DSN64BU", "B002KKCZLA",
     "B0055ATVV4", "B00HFY47JY", "B00DPHJUTW", "B008FWRJ6I", "B003Z4JUFO", "B00JFB4RL8", "B00HR1LTNM",
     "B005HMTPBG", "B00KCT84I4", "B00HXDITEG"],
     "bought_together": ["B003O2SLXY", "B003H4QFVY", "B002R8JPTK", "B000EDTVYY"]},
     "salesRank": {"Shoes": 18},
     "categories": [["Clothing, Shoes & Jewelry", "Shoes & Accessories: International Shipping Available"],
     ["Clothing, Shoes & Jewelry", "K", "Keen"], ["Clothing, Shoes & Jewelry", "Comfort Shoes"],
     ["Clothing, Shoes & Jewelry", "Men", "Shoes", "Sandals"],
     ["Clothing, Shoes & Jewelry", "Men", "Shoes", "Athletic", "Sport Sandals"]]} 
]}

另外,我认为它的问题不是vaild json它在每个产品的最后它没有逗号后面的花括号'},'是唯一的原因还是任何人都可以看到另一个?

2 个答案:

答案 0 :(得分:0)

我设法做到了,我使用Atoms查找和替换功能并将所有Parse.Cloud.afterSave("Channel", function(request) { var channel = request.object; var channel_orig = request.original; if (channel.get("status") != channel_orig.get("status")) { // Send push notification. } }); 替换为]]}无需编码:)

答案 1 :(得分:-2)

我将您的两个示例产品放入名为data.json的文件中,然后在其旁边创建一个python3脚本fixjson.py

#!/usr/bin/python
import json 
import pprint

with open("data.json", "r") as f:

    data = ""
    for line in f.readlines():
        data += line.strip()

    data = data.replace("}{\"asin", "},{\"asin")
    products = json.loads(data)
    pprint.pprint(products)

with open("newdata.json", "w") as w:
    w.write(json.dumps(products, indent=2))

使用python3 fixjson.py运行脚本。它应该通过在产品之间插入缺少的,来修复损坏的JSON,并将具有2的意图级别的数据写入名为newdata.json的文件中。

注意:这只是一种快速而肮脏的方法。

编辑:为什么我会被投票? OP要求一种比手动修复文件更好的方法,我提供了一个片段,可以根据给定的信息修复JSON问题。