generated from Hazel/python-project
Add Home
commit
6593843cd3
45
Home.md
Normal file
45
Home.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# How it works
|
||||||
|
|
||||||
|
## create object with json string as value
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> o = {"foo": "foo"}
|
||||||
|
>>> oo = {"bar": json.dumps(o)}
|
||||||
|
>>> oo
|
||||||
|
{'bar': '{"foo": "foo"}'}
|
||||||
|
>>> json.dumps(oo)
|
||||||
|
'{"bar": "{\\"foo\\": \\"foo\\"}"}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## extract the escaped string
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> s = json.dumps(oo)[9:-2]
|
||||||
|
>>> s
|
||||||
|
'{\\"foo\\": \\"foo\\"}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## reproduce error
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> r = json.loads(s)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "<stdin>", line 1, in <module>
|
||||||
|
File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
|
||||||
|
return _default_decoder.decode(s)
|
||||||
|
File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
|
||||||
|
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
|
||||||
|
File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode
|
||||||
|
obj, end = self.scan_once(s, idx)
|
||||||
|
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
|
||||||
|
```
|
||||||
|
|
||||||
|
## solve error by nesting json
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> r = json.loads('{"foo": "' + s + '"}')
|
||||||
|
>>> r
|
||||||
|
{'foo': '{"foo": "foo"}'}
|
||||||
|
>>> json.loads(r["foo"])
|
||||||
|
{'foo': 'foo'}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user