Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence

前端 未结 12 962
说谎
说谎 2020-11-21 23:25

sample code:

>>> import json
>>> json_string = json.dumps(\"ברי צקלה\")
>>> print json_string
\"\\u05d1\\u05e8\\u05d9 \\u05e6\\u05         


        
12条回答
  •  故里飘歌
    2020-11-22 00:26

    If you are loading JSON string from a file & file contents arabic texts. Then this will work.

    Assume File like: arabic.json

    { 
    "key1" : "لمستخدمين",
    "key2" : "إضافة مستخدم"
    }
    

    Get the arabic contents from the arabic.json file

    with open(arabic.json, encoding='utf-8') as f:
       # deserialises it
       json_data = json.load(f)
       f.close()
    
    
    # json formatted string
    json_data2 = json.dumps(json_data, ensure_ascii = False)
    

    To use JSON Data in Django Template follow below steps:

    # If have to get the JSON index in Django Template file, then simply decode the encoded string.
    
    json.JSONDecoder().decode(json_data2)
    

    done! Now we can get the results as JSON index with arabic value.

提交回复
热议问题