1

Here's my python module:

#!/usr/bin/python

import rpy2.robjects as robjects
import MySQLdb as mdb
import json
import rpy2.robjects.vectors as ro
r= robjects.r

class Deviation:
    def __init__(self):
        print("Standard Deviation")

    def s_deviation(self):
        con = mdb.connect('localhost', 'root', 'devil', 'data')        
        cursor = con.cursor()
        cursor.execute("SELECT * FROM traffic")
        rows = cursor.fetchall()
        rows = list(rows)
        a = [x[1] for x in rows]
        result = ro.IntVector(a)
        a = r.sd(result)
        print a

def deviation():
    dev = Deviation()
    deviation = dev.s_deviation()

The function deviation() from this module when called returns the result as R vector. How do I make this return the result as Json objects? Thank you!

1
  • 4
    None of your functions return anything. Commented Aug 29, 2012 at 8:36

1 Answer 1

1

Use json.dumps or simplejson.dumps (depending on your python version). You use it like this:

>> print( simplejson.dumps( { "dev": dev.s_deviation() } ) )
'{"dev":1.456}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.