How to decode an invalid json string in python

后端 未结 4 1399
自闭症患者
自闭症患者 2020-12-18 03:57

I wonder if there is a way to decode a JSON-like string.

I got string:

\'{ hotel: { id: \"123\", name: \"hotel_name\"} }\'

It\'s no

4条回答
  •  有刺的猬
    2020-12-18 04:41

    You could try and use a wrapper for a JavaScript engine, like pyv8.

    import PyV8
    ctx = PyV8.JSContext()
    ctx.enter()
    # Note that we need to insert an assignment here ('a ='), or syntax error.
    js = 'a = ' + '{ hotel: { id: "123", name: "hotel_name"} }'
    a = ctx.eval(js)
    a.hotel.id
    >> '123' # Prints
    

提交回复
热议问题