When parsing JSON from ASCII-encoded text files in Python 2, the default behavior is to represent string values as Unicode objects. This can pose a problem when working with libraries that only accept string objects.
Fortunately, there are several ways to obtain string objects instead of Unicode objects:
PyYAML is a Python library for parsing YAML, which is a superset of JSON. It provides a convenient way to get string objects while preserving the JSON structure:
<code class="python">import yaml json_string = '["a", "b"]' data = yaml.safe_load(json_string) # Returns a list of strings</code>
Note: This method only works for ASCII-encoded data.
If the data is Unicode-encoded, you can use a conversion function to convert the Unicode strings to regular strings:
<code class="python">def unicode_to_string(obj): if isinstance(obj, basestring): return obj.encode('utf-8') elif isinstance(obj, dict): return dict((unicode_to_string(k), unicode_to_string(v)) for k, v in obj.iteritems()) elif isinstance(obj, list): return [unicode_to_string(v) for v in obj] else: return obj # Usage: json_string = '["\u00e1", "\u00e9"]' data = json.loads(json_string) converted_data = unicode_to_string(data) # Converts Unicode strings to strings</code>
You can also use an object hook to convert Unicode strings to strings during serialization:
<code class="python">import json def object_hook(obj): if isinstance(obj, unicode): return obj.encode('utf-8') return obj # Usage: json_string = '["a", "b"]' data = json.loads(json_string, object_hook=object_hook) # Converts Unicode strings to strings during deserialization</code>
The above is the detailed content of How to Get String Objects Instead of Unicode from JSON in Python 2?. For more information, please follow other related articles on the PHP Chinese website!