Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将java程序作为子进程调用时,如何传递字典?_Java_Python_Dictionary - Fatal编程技术网

在python中将java程序作为子进程调用时,如何传递字典?

在python中将java程序作为子进程调用时,如何传递字典?,java,python,dictionary,Java,Python,Dictionary,我正在尝试使用python调用java jar程序,当我将输入参数作为字符串传递时,该程序运行良好,现在我需要将dictionary作为输入参数传递 代码如下: Python: class ExecuteKTR(): def GET(self,r): web.header('Access-Control-Allow-Origin','*') web.header('Access-Control-Allow-Credentials', 'true')

我正在尝试使用python调用java jar程序,当我将输入参数作为字符串传递时,该程序运行良好,现在我需要将dictionary作为输入参数传递

代码如下:

Python:

class ExecuteKTR():
     def GET(self,r):
      web.header('Access-Control-Allow-Origin','*')
      web.header('Access-Control-Allow-Credentials', 'true')
      secToken = web.input().SecurityToken
      Domain = web.input().Domain
      fieldnames = ast.literal_eval(web.input().fieldnames)
      authResult = Auth.GetSession(secToken,Domain)
      if authResult.reason == "OK":
        args = ['ktrjob.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file
        result = jarWrapper(*args)
        print result
      elif authResult.reason == 'Unauthorized':
         result = comm.format_response(False,authResult.reason,"Check the custom message",exception=None)
         return result

def jarWrapper(*args):
    process = Popen(['java', '-jar']+list(args), stdout=PIPE, stderr=PIPE)
    ret = []
    while process.poll() is None:
        line = process.stdout.readline()
        print line
        if line != '' and line.endswith('\n'):
            ret.append(line[:-1])
    stdout, stderr = process.communicate()
    ret += stdout.split('\n')
    if stderr != '':
        ret += stderr.split('\n')
    ret.remove('')
    return ret
   String file = "";         
        if(args.length != 0)
        {
            file = args[0];
            for ( int i=0;i<=args.length;i++)
            {                   
                System.out.print(args[i]);
            }
        }
Java:

class ExecuteKTR():
     def GET(self,r):
      web.header('Access-Control-Allow-Origin','*')
      web.header('Access-Control-Allow-Credentials', 'true')
      secToken = web.input().SecurityToken
      Domain = web.input().Domain
      fieldnames = ast.literal_eval(web.input().fieldnames)
      authResult = Auth.GetSession(secToken,Domain)
      if authResult.reason == "OK":
        args = ['ktrjob.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file
        result = jarWrapper(*args)
        print result
      elif authResult.reason == 'Unauthorized':
         result = comm.format_response(False,authResult.reason,"Check the custom message",exception=None)
         return result

def jarWrapper(*args):
    process = Popen(['java', '-jar']+list(args), stdout=PIPE, stderr=PIPE)
    ret = []
    while process.poll() is None:
        line = process.stdout.readline()
        print line
        if line != '' and line.endswith('\n'):
            ret.append(line[:-1])
    stdout, stderr = process.communicate()
    ret += stdout.split('\n')
    if stderr != '':
        ret += stderr.split('\n')
    ret.remove('')
    return ret
   String file = "";         
        if(args.length != 0)
        {
            file = args[0];
            for ( int i=0;i<=args.length;i++)
            {                   
                System.out.print(args[i]);
            }
        }

您可以使用JSON将字典作为字符串传递,因为在Python字典和JSON中基本相同

您可以使用JSON将字典作为字符串传递,因为在Python字典和JSON中基本相同

Java只允许字符串数组作为程序的输入参数,正如Java中main方法的签名所示:

public class MainClass {

    public static void main(final String[] args)
    {
        //do stuff
    }
}
您需要将字典翻译成一个(或多个)字符串作为java程序的输入,并在java程序中解码传输的字符串。一种解决方案是将字典编写为JSON字符串,并从Java程序中提供的字符串解析JSON对象

要在python中创建JSON字符串,可以使用

strJSON = json.dumps(myDict)

在Java中,有几个库可以从Json格式导入。

Java只允许字符串数组作为程序的输入参数,如Java中主方法的签名所示:

public class MainClass {

    public static void main(final String[] args)
    {
        //do stuff
    }
}
您需要将字典翻译成一个(或多个)字符串作为java程序的输入,并在java程序中解码传输的字符串。一种解决方案是将字典编写为JSON字符串,并从Java程序中提供的字符串解析JSON对象

要在python中创建JSON字符串,可以使用

strJSON = json.dumps(myDict)
在Java中,有几个库可以从Json格式导入。

将字典传递给程序是毫无意义的:传递给子进程的只是字符串

所以你必须选择一个惯例。如果您需要能够传递任意对象,JSON是一个选项,因为两种语言都有库。如果字典非常简单并且总是具有相同的结构,那么也可以只传递变量部分。但请注意,JSON不指定任何日期格式,因此您必须选择自己的格式,并且在Python和Java端保持一致。

将字典传递给程序是毫无意义的:传递给子进程的只是字符串


所以你必须选择一个惯例。如果您需要能够传递任意对象,JSON是一个选项,因为两种语言都有库。如果字典非常简单并且总是具有相同的结构,那么也可以只传递变量部分。但是请注意,JSON不指定任何日期格式,因此您必须选择自己的格式,并且在Python和Java端保持一致。

它们是否相同并不重要,重要的是Java和Python中都存在用于序列化和反序列化JSON的库。这是一行简单的
strJSON=JSON.dumps(myDict)
myDict\u recostruct=json.loads(strJSON)
为他的案例。但你是对的。它们是否相同并不重要,重要的是Java和Python中都存在用于序列化和反序列化JSON的库。对于他的情况,这是一行简单的
strJSON=JSON.dumps(myDict)
myDict\u recostruct=JSON.loads(strJSON)
。但你是对的,问题是java程序如何接受数据。没有通用的传递口述的方法。一种解决方案可以是将dict序列化为JSON,并使用显式文件(讨厌,需要清理)或通过stdout传递它。但这两者都需要java应用程序的协作,问题是java程序如何接受数据。没有通用的传递口述的方法。一种解决方案可以是将dict序列化为JSON,并使用显式文件(讨厌,需要清理)或通过stdout传递它。但这两者都需要java应用程序的协作。如果可以,您可以提供一个示例吗¶meters={“vehicle_usage”:1,“vehicle_type”:2,“vehicle_class”:3}&ReportName=calldetail¶meters={“vehicle_usage”:1,“vehicle_type”:2,“vehicle_class”:3}&ReportName=calldetail