简体   繁体   English

仅使用默认库在jython中解码json

[英]Decoding json in jython using only default libraries

I have a question about getting json into my jython script. 我有一个关于将json放入我的jy​​thon脚本的问题。 Here's my scenario: 这是我的情景:

  • I am running a python app on my laptop 我正在我的笔记本电脑上运行python应用程序
  • That app needs to share data with a jython app running in a hosted environment out in a 3rd party's cloud environment. 该应用程序需要与第三方云环境中托管环境中运行的jython应用程序共享数据。
  • I have no ability to add 3rd party modules to this environment (so I can't install com.xhaus.jyson for example) 我无法将第三方模块添加到此环境中(因此我无法安装com.xhaus.jyson)
  • This probably means I'm limited to features that are native to java - org.json.JSONObject perhaps 这可能意味着我可能只限于java-org.json.JSONObject本机的功能

So with those limitations, I want to take a dictionary object on my laptop, turn it into json, deliver it to the hosted jython app and then use the native jython or java tools to turn it back into that dictionary object so I can continue working on it in my script hosted in the cloud. 因此,有了这些限制,我想在我的笔记本电脑上使用字典对象,将其转换为json,将其传递给托管的jython应用程序,然后使用本机jython或java工具将其转换回该字典对象,以便我可以继续工作在我托管在云端的脚本中。

I already know how to do this in "regular" python. 我已经知道如何在“常规” python中执行此操作。 It's easy. 这很容易。 import json and go nuts. 导入json并疯狂。 But my java kung fu is weak and I've never worked in jython before. 但我的java功夫很弱,我以前从未在jython工作过。

So I'm trying to figure out if this if it's possible to do this reliably and easily using the java underlying the jython or if I'd be better off using something like ast and just send the dictionary as a string literal. 所以我试图弄清楚这是否可以使用jython底层的java可靠而轻松地做到这一点,或者如果我最好使用像ast这样的东西,只需将字典作为字符串文字发送。 I'd honestly prefer to stick with json for all the normal reasons people like json so any help with leveraging the java libraries to do this work would be appreciated. 我真的更喜欢坚持使用json,因为人们喜欢json的所有正常原因,所以任何帮助利用java库来完成这项工作将不胜感激。

I forgot about this question. 我忘记了这个问题。 My core problem here was that I was using a 3rd party cloud offering and they were the owners of the Jython install so I was limited in what I could change about the Jython environment. 我的核心问题是我使用的是第三方云产品,他们是Jython安装的所有者,因此我对Jython环境的改变有限。 At the time I was thinking I could leverage a JAVA library that would be available to jython to solve this problem but that never worked out. 当时我在想我可以利用一个JAVA库来解决这个问题但是从未解决过这个问题。

While jython was out of my control I did control how I sent the data so instead of using JSON, I sent formatted strings and then used the python ast library, which was in jython, to turn those strings into python objects. 虽然jython不受我的控制,但我确实控制了我发送数据的方式而不是使用JSON,我发送了格式化字符串,然后使用python ast库(在jython中)将这些字符串转换为python对象。

In the end it looked something like this: 最后它看起来像这样:

thestring = """['name', 'quest', 'favorite color']"""
theobject = ast.literal_eval(thestring)

That type of logic let my python script on my local machine post strings to a web app running jython and convert those strings into python data types and then use them. 这种类型的逻辑让我的本地机器上的python脚本将字符串发布到运行jython的web应用程序,并将这些字符串转换为python数据类型然后使用它们。 It was exactly what I wanted to do with JSON without actually using JSON - it was python dicts so it looked an awful lot like JSON if you weren't really paying attention. 这正是我想要用JSON而不实际使用JSON的原因 - 它是python dicts所以它看起来非常像JSON,如果你没有真正关注。

Thanks everybody for your suggestions. 谢谢大家的建议。

You could use Jackson or GSON . 你可以使用JacksonGSON You might use anything listed on JSON.org under Java, you might be able to use the stuff under "Python" (for example simplejson ). 您可以在Java下使用JSON.org上列出的任何内容,您可以使用“Python”下的内容(例如simplejson )。

You could use simplejson , which can be used as pure python, so will run on Jython. 你可以使用simplejson ,它可以用作纯python,因此可以在Jython上运行。 By including it in the same source folder as your other code, there's no need for a special installation. 通过将其包含在与其他代码相同的源文件夹中,无需进行特殊安装。

Jyson seems to be an open source project implementing a python complaint json codec in pure Java. Jyson似乎是一个开源项目,在纯Java中实现python投诉json编解码器。

Download it here: http://opensource.xhaus.com/projects/jyson/files . 在这里下载: http//opensource.xhaus.com/projects/jyson/files

Then unzip and add jyson-1.0.2/lib/jyson-1.0.2.jar to your CLASSPATH. 然后解压缩并将jyson-1.0.2 / lib / jyson-1.0.2.jar添加到CLASSPATH。

Then import like this: 然后像这样导入:

 import com.xhaus.jyson.JysonCodec as json

Found this info here: http://aholzner.wordpress.com/2010/07/31/using-json-from-jython/ . 在此处找到此信息: http//aholzner.wordpress.com/2010/07/31/using-json-from-jython/ Works for me. 适合我。

This question is a bit dated, but I came across this when having a similar issue: 这个问题有点过时了,但是在遇到类似问题时我遇到了这个问题:

https://support.xebialabs.com/hc/communities/public/questions/201998425-Use-json-with-Jython-script https://support.xebialabs.com/hc/communities/public/questions/201998425-Use-json-with-Jython-script

In essence, this is how I solved it (using simplejson ): 从本质上讲,这就是我解决它的方法(使用simplejson ):

try:
    sys.path.append('<PATH TO SIMPLEJSON ROOT>')
    import simplejson as json
except Exception, e:
    print e

If you're using the latest version of jython , you have access to the python json library. 如果您使用的是最新版本的jython ,则可以访问python json库。 So: 所以:

import json
mydict_as_json = json.dumps(mydict)
# send over the wire
# on the remote side
import json
mydict = json.load(mydict_as_json_from_remote_as_file_like_object)

Hope that helps... 希望有帮助......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM