0

I got json data using opener.open method. Now I want to reference its elmentS. I tried the following code but i get error! Furthermore , I want to get the value of token= only for the link2 . Could any one help me fix this error and get value of token ? Thanks in advance.

code:

   resp2 = opener.open('http://somewebsite.com/test/id',post_data)
      print resp2.read()
      Response = resp2.read();
      j_obj = json.load(Response)
      print j_obj['link2']

error:

ERROR: EXCEPTION Thrown (PythonToCppException) : 
-->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.AttributeError'>
Error Contents: 'str' object has no attribute 'read'
j_obj = json.load(Response)
 line 286, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
 -->End of Python script error report<--

json data :

{
        "id": 1,
        "name": "Test World",
        "link1": "rtmp:\/\/me.someWebsite.com:1234\/static\/testWorld1?token=123456789abcdefghijklmnopqr&e=987654321&u=99999",
        "link2": "http:\/\/me.someWebsite.com:1234\/testWorld1\/index.m3u8?token=123456789abcdefghijklmnopqr&e=987654321&u=99999&channel=testWorld1",
        "image": "http:\/\/me.someWebsite.com\/img\/1\/2\/3\/4\/56.png",
        "net": "rtmp:\/\/me.someWebSite.com:1234\/static",
        "url": "testWorld1?token=123456789abcdefghijklmnopqr&e=987654321&u=99999",
        "favorite": false,
        "date": "2014-05-1"
    }
user1788736
  • 2,727
  • 20
  • 66
  • 110

2 Answers2

1

Do the following - note that resp2 is already a string!

resp2 = opener.open('http://somewebsite.com/test/id',post_data)
print resp2  # You can verify you are receiving JSON data here.
j_obj = json.loads(resp2)
print j_obj['link2']
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Thanks for replies. I tried it now i get this error now :Error Type: Error Contents: No JSON object could be decoded Traceback (most recent call last): – user1788736 Jan 13 '16 at 04:06
  • print the string you've received. Post what it contains. – Martin Konecny Jan 13 '16 at 04:07
  • Many thanks i removed Response = resp2.read(); and used resp2 now i got the value of link2 . How i can get the value of token on that link2 ?(token=123456789abcdefghijklmnopqr) – user1788736 Jan 13 '16 at 04:15
  • 1
    You will need to use a regular expression. Try `import re; re.search(r"token=(\w+)", j_obj['url']).group(1)` – Martin Konecny Jan 13 '16 at 04:19
  • That happens if the `url` doesn't have token. Make sure you verify that it does. – Martin Konecny Jan 13 '16 at 04:29
  • I got the token but why if I use print resp2 instead of resp2.read() i get this :> instead of actual value of json for verification ?And if i use print resp2.read() i get this error :Error Contents: No JSON object could be decoded ? – user1788736 Jan 13 '16 at 04:31
  • I don't think this code works. Why is this the accepted answer? – tripleee Jan 13 '16 at 04:53
1

You could try using a different method,

  import urllib2

  post_data = ...
  fp = urllib2.urlopen('http://somewebsite.com/test/id', post_data)
  resp = fp.read()
  print(resp)
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61