Jython:解析JSON对象以获取值(对象具有数组) [英] Jython: Parse JSON object to get value (object has array)

查看:743
本文介绍了Jython:解析JSON对象以获取值(对象具有数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JavaScript可以解析 JSON对象(对象具有数组)并返回ZONE字段中的值.

I have JavaScript that parses a JSON object (object has array) and returns the value from the ZONE field.

var obj = JSON.parse(json_text);
parsed_val = obj.features[0].attributes.ZONE

我想将JavaScript代码转换为 Jython .

这是我尝试过的:

from com.ibm.json.java import JSONObject

obj = JSONObject.parse(json_text)
parsed_val = obj.get('features.attributes.ZONE'); 

Jython会编译,但是不会返回有效值(它会返回None).我认为这是因为我没有正确引用数组.

The Jython compiles, but it doesn't return a valid value (it returns None). I think this is because I haven't referenced the array properly.

如何使用Jython 解析JSON对象/数组以获取ZONE值?

How can I parse the JSON object/array using Jython to get the ZONE value?

(Jython版本是2.7.0.但是,我似乎无法使用Python的JSON库(通常包含在Jython中)).

推荐答案

我需要在对象的每个级别使用get().

I needed to use get() at each level of the object.

以及指定数组在第一级之后的索引位置:[0].

As well as specify the array's index position after the first level: [0].

from com.ibm.json.java import JSONObject

obj = JSONObject.parse(json_text)
parsed_val = obj.get("features")[0].get("attributes").get("WEEK")

信用会发送给 @vikarjramun ,以指示我正确的方向.谢谢.

Credit goes to @vikarjramun for pointing me in the right direction. Thanks.

这篇关于Jython:解析JSON对象以获取值(对象具有数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆