如何使用Python验证具有转义引号的JSON字符串

huangapple go评论112阅读模式
英文:

How to validate a JSON string with escaped quotes using Python

问题

I am using json.loads to parse a JSON string. However, it identifies the string as invalid JSON when it contains escaped double quotes. Since the string itself is valid, how could I parse it correctly without modifying the input string (i.e. using \" instead of "). Here is my code:

  1. import json
  2. a = '{"name":"Nickname \\"John\\" Doe", "age":31, "Salary":25000}'
  3. print("initial strings given - \n", a)
  4. try:
  5. json_object1 = json.loads(a)
  6. print("Is valid json? true")
  7. except ValueError as e:
  8. print("Is valid json? false")
英文:

I am using json.loads to parse a JSON string. However, it identifies the string as invalid JSON when it contains escaped double quotes. Since the string itself is valid, how could I parse it correctly without modifying the input string (i.e. using \" instead of "). Here is my code:

  1. import json
  2. a = '{"name":"Nickname \"John\" Doe", "age":31, "Salary":25000}'
  3. print ("initial strings given - \n", a)
  4. try:
  5. json_object1 = json.loads(a)
  6. print ("Is valid json? true")
  7. except ValueError as e:
  8. print ("Is valid json? false")

Thanks!

答案1

得分: 4

Since the backslash itself is an escape character, you need to either escape it, or use a raw string (simply with the "r" prefix):

a = '{ "name":"Nickname \"John\" Doe", "age":31, "Salary":25000}'

or

a = r'{ "name":"Nickname "John" Doe", "age":31, "Salary":25000}'

英文:

Since the backslash itself is an escape character, you need to either escape it, or use a raw string (simply with the r prefix):

  1. a = '{"name":"Nickname \\"John\\" Doe", "age":31, "Salary":25000}'

or

  1. a = r'{"name":"Nickname \"John\" Doe", "age":31, "Salary":25000}'

答案2

得分: 1

这是需要转义的 \ 以生成有效的 json

  1. #soJsonEscapeQuotes
  2. import json
  3. a = '{ "name":"Nickname \\"John\\" Doe", "age":31, "Salary":25000}'
  4. print ("initial strings given - \n", a)
  5. try:
  6. json_object1 = json.loads(a)
  7. print ("Is valid json? true")
  8. except ValueError as e:
  9. print ("Is valid json? false")

输出:

  1. initial strings given -
  2. { "name":"Nickname \"John\" Doe", "age":31, "Salary":25000}
  3. Is valid json? true
英文:

Its the \ that need escaping to make valid json:

  1. #soJsonEscapeQuotes
  2. import json
  3. a = '{"name":"Nickname \\"John\\" Doe", "age":31, "Salary":25000}'
  4. print ("initial strings given - \n", a)
  5. try:
  6. json_object1 = json.loads(a)
  7. print ("Is valid json? true")
  8. except ValueError as e:
  9. print ("Is valid json? false")

Output:

  1. initial strings given -
  2. {"name":"Nickname \"John\" Doe", "age":31, "Salary":25000}
  3. Is valid json? true

huangapple
  • 本文由 发表于 2020年1月4日 00:33:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定