Python / Json : 큰 따옴표로 묶인 속성 이름 예상
Python에서 JSON 개체를로드하는 좋은 방법을 찾으려고 노력해 왔습니다. 이 json 데이터를 보냅니다.
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
백엔드에 문자열로 수신 한 다음 json.loads(data)
구문 분석하는 데 사용 했습니다.
하지만 매번 같은 예외가 발생했습니다.
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
나는 그것을 봤지만 json.loads(json.dumps(data))
json 형식이 아닌 데이터조차도 모든 종류의 데이터를 받아들이 기 때문에 개인적으로 효율적이지 않은 이 솔루션 외에는 아무것도 작동하지 않는 것 같습니다 .
어떤 제안이라도 대단히 감사하겠습니다.
이:
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
JSON이 아닙니다.
이:
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
JSON입니다.
JSON은 큰 따옴표로 문자열을 묶는 것만 허용하므로 다음과 같이 문자열을 조작 할 수 있습니다.
str = str.replace("\'", "\"")
이렇게하면 JSON 문자열에서 모든 작은 따옴표가 큰 따옴표로 바뀝니다 str
.
js-beautify
덜 엄격한 것을 사용할 수도 있습니다 .
$ pip install jsbeautifier
$ js-beautify file.js
JSON 문자열은 큰 따옴표를 사용해야합니다. JSON python 라이브러리는이를 적용하므로 문자열을로드 할 수 없습니다. 데이터는 다음과 같아야합니다.
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
그것이 당신이 할 수있는 일이 아니라면 ast.literal_eval()
대신 사용할 수 있습니다.json.loads()
간단히 말해서이 문자열은 유효한 JSON이 아닙니다. 오류에서 알 수 있듯이 JSON 문서는 큰 따옴표를 사용해야합니다.
데이터 소스를 수정해야합니다.
JSON 데이터를 확인했습니다.
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
에서 http://jsonlint.com/ 과 결과는 :
Error: Parse error on line 1:
{ 'http://example.org/
--^
Expecting 'STRING', '}', got 'undefined'
다음 문자열로 수정하면 JSON 오류가 해결됩니다.
{
"http://example.org/about": {
"http://purl.org/dc/terms/title": [{
"type": "literal",
"value": "Anna's Homepage"
}]
}
}
제 경우에는 큰 따옴표가 문제가되지 않았습니다.
마지막 쉼표는 동일한 오류 메시지를주었습니다.
{'a':{'b':c,}}
^
이 쉼표를 제거하기 위해 간단한 코드를 작성했습니다.
import json
with open('a.json','r') as f:
s = f.read()
s = s.replace('\t','')
s = s.replace('\n','')
s = s.replace(',}','}')
s = s.replace(',]',']')
data = json.loads(s)
그리고 이것은 나를 위해 일했습니다.
이 방법을 사용하여 원하는 출력을 얻었습니다. 내 스크립트
x = "{'inner-temperature': 31.73, 'outer-temperature': 28.38, 'keys-value': 0}"
x = x.replace("'", '"')
j = json.loads(x)
print(j['keys-value'])
산출
>>> 0
As it clearly says in error, names should be enclosed in double quotes instead of single quotes. The string you pass is just not a valid JSON. It should look like
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
I had similar problem . Two components communicating with each other was using a queue .
First component was not doing json.dumps before putting message to queue. So the JSON string generated by receiving component was in single quotes. This was causing error
Expecting property name enclosed in double quotes
Adding json.dumps started creating correctly formatted JSON & solved issue.
ReferenceURL : https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes
'Development Tip' 카테고리의 다른 글
Android 및 Gradle에서 SimpleXML 사용 (0) | 2020.12.28 |
---|---|
디버그 빌드 변형에만 Stetho 포함 (0) | 2020.12.28 |
Javascript-setInterval에게 x 번만 실행하도록 지시합니까? (0) | 2020.12.28 |
C ++ 문자열을 16 진수로 또는 그 반대로 변환 (0) | 2020.12.28 |
JSON 문자열을 Javascript의 JSON 객체 배열로 변환 (0) | 2020.12.28 |