Jython: Parse JSON object to get value (object has array)

久未见 提交于 2019-12-08 11:19:14

问题


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

I would like to convert the JavaScript code to Jython.

This is what I've tried:

from com.ibm.json.java import JSONObject

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

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.

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

(Jython version is 2.7.0. However, I can't seem to use Python's JSON library (normally included in Jython)).


回答1:


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

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")

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



来源:https://stackoverflow.com/questions/57617494/jython-parse-json-object-to-get-value-object-has-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!