godwinjk / Json_Parser

MIT License
25 stars 7 forks source link

Version 1.7.1 reported an error in Pycharm #21

Open Fan-CR opened 11 months ago

Fan-CR commented 11 months ago

It cannot convert the dict data type to the JSON data type in Python IDE (Pycharm) It shows the error: Unexpected character (''' (code 39)): was expecting double-quote to start field

SadSack963 commented 11 months ago

This is not a problem with Json_Parser. JSON strings must be enclosed in double quotes. See Introducing JSON Unfortunately, Python has a tendency to enclose the strings in single quotes instead - at least, in Windows. I'm not sure if the issue is with Python, requests or the Operating system / Terminal. Your best bet is to run the JSON through a text editor and search/replace the single quotes with double quotes. Be careful not to replace single quotes inside a string though. For a dictionary, try this:

import json
my_dict = {
    'a': 1,
    'b': 2,
    'c': 3,
}
print(json.dumps(my_dict))

One option that seems to work for me when getting data from a request (I cannot guarantee in all situations):

import requests
import json
import ast
...
response = requests.get(url=api_url, params=api_params, headers=api_headers)
print(json.dumps(ast.literal_eval(response.text)))

HTH

MiTuLittle commented 10 months ago

This question was find in the latest version(1.7.1), and the single quotes in string won't reported Error in Pychram at the previous version