问题
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