python - Strip leading 'u' from JSON for PHP -


i have following i'm trying parse in php

it's json leading 'u' (probably python indicators, i'm not sure) what's quickest way turn these valid json? or valid json php should able parse?

{u'_id': u'fruit',                u'etags': [{u'score': 3.612, u'tag': u'apple'},                           {u'score': 1.443, u'tag': u'banana'},                           {u'score': -0.833, u'tag': u'cherry'},                           {u'score': -2.048, u'tag': u'orange'}]} 

edit: "syntax error, malformed json" in php off, may not 'u'

edit: not great answer, getting trick done me:

$json = str_replace("u'", "'", $json); $json = str_replace("'", '"', $json); 

you're doing wrong here. assuming have in python

data = {u'_id': u'fruit',            u'etags': [{u'score': 3.612, u'tag': u'apple'},                       {u'score': 1.443, u'tag': u'banana'},                       {u'score': -0.833, u'tag': u'cherry'},                       {u'score': -2.048, u'tag': u'orange'}]) 

you're doing equivalent of

print repr(data) 

you should doing:

import json print json.dumps(data) 

the string you're parsing not json because it:

  • has u shouldn't
  • uses ' instead of "
  • does not encode none null
  • ...

since not control data source, have no choice invoke python interpreter on input:

import ast import json  bad_json = get_from_server() data = ast.literal_eval(bad_json) print json.dumps(data) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -