Javascript - cut occurrences of string u in JSON string, parsing returns unexpected token

I have a text field in a database that contains the results of a python operation json.dumps(list_instance). Thus, the internal fields are prefixed u'and violate the browser function JSON.parse().

Example JSON string:

"density": "{u'Penobscot': 40.75222856500098, u'Sagadahoc': 
  122.27083333333333, u'Lincoln': 67.97977755308392, u'Kennebec': 
  123.12237174095878, u'Waldo': 48.02117802779616, u'Cumberland':  
  288.9285325791363, u'Piscataquis': 3.9373586457405247, u'Hancock': 
  30.698239582715903, u'Washington': 12.368718341168325, u'Aroostook': 
  10.827378163074039, u'York': 183.47612497543722, u'Franklin':  
  16.89330963710371, u'Oxford': 25.171240748402518, u'Somerset': 
  12.425648288323485, u'Knox': 108.48302300109529, u'Androscoggin': 
  208.75502815768303}"

What I would like to do is replace those occurrences u'with '(single quote). I tried

function renderValues(data){
   var pop = JSON.parse(data.density.replace(/u'/g, "'"));
}

but I always get an exception unexpected token '. Since many of the possible key fields may contain u, you cannot simply delete this character. How can I find all instances u'and replace with 'no exception?

+4
4

: replace(/u'/g, "'")); = > replace(/u'(?=[^:]+')/g, "'"));.

:

"{u'Penobscot': 40.75222856500098, u'Sagadahoc': 122.27083333333333, u'Lincoln': 67.97977755308392, u'Kennebec': 123.12237174095878, u'Waldo': 48.02117802779616, u'Cumberland': 288.9285325791363, u'Piscataquis': 3.9373586457405247, u'Hancock': 30.698239582715903, u'Timbuktu': 12.368718341168325, u'Aroostook': 10.827378163074039, u'York': 183.47612497543722, u'Franklin': 16.89330963710371, u'Oxford': 25.171240748402518, u'Somerset': 12.425648288323485, u'Knox': 108.48302300109529, u'Androscoggin': 208.75502815768303}".replace(/u'(?=[^:]+')/g, "'");

:

"{'Penobscot': 40.75222856500098, 'Sagadahoc': 122.27083333333333, 'Lincoln': 67.97977755308392, 'Kennebec': 123.12237174095878, 'Waldo': 48.02117802779616, 'Cumberland': 288.9285325791363, 'Piscataquis': 3.9373586457405247, 'Hancock': 30.698239582715903, 'Timbuktu': 12.368718341168325, 'Aroostook': 10.827378163074039, 'York': 183.47612497543722, 'Franklin': 16.89330963710371, 'Oxford': 25.171240748402518, 'Somerset': 12.425648288323485, 'Knox': 108.48302300109529, 'Androscoggin': 208.75502815768303}"

+3

. , JSON. -JSON .

, python, JSON, , str() unicode(), . , "", - , python - :

{"density": "a string that looks like JSON but it is in fact a string reprensentation of a dictionary"}

json.dumps JSON.

, - .

. , u '...' python- unicode JSON.

>>> import json
>>> data = {u'name': u'Manuel', u'age': 26}
>>> print data
{u'age': 26, u'name': u'Manuel'}  # this is the python representation of a dictionary
>>> print json.dumps(data)
{"age": 26, "name": "Manuel"} # this is a valid JSON string

JSON , .

+4

, , :

var strExample = {'att1':u'something with u'};

strExample.replace(/u'[\}|\,]/g, "ç'").replace(/u'/g, "'").replace(/ç'/g, "u'");

//{'att1':'something with u'};

u ', , "ç"

u , , u ',

0

, , u, .

replace(/(?!\s|:)((u)(?='))/g, "")

, , .

, OP "u" , , :)

0

Source: https://habr.com/ru/post/1523419/


All Articles