Python: parsing JSON-like Javascript data structures (w/ consecutive commas)

前端 未结 6 1812
南旧
南旧 2020-12-10 22:55

I would like to parse JSON-like strings. Their lone difference with normal JSON is the presence of contiguous commas in arrays. When there are two such commas, it i

6条回答
  •  天涯浪人
    2020-12-10 23:25

    For those looking for something quick and dirty to convert general JS objects (to dicts). Some part of the page of one real site gives me some object I'd like to tackle. There are 'new' constructs for dates, and it's in one line, no spaces in between, so two lines suffice:

    data=sub(r'new Date\(([^)])*\)', r'\1', data)
    data=sub(r'([,{])(\w*):', r'\1"\2":', data)
    

    Then json.loads() worked fine. Your mileage may vary:)

提交回复
热议问题