java 解析 json 索引对象,Jython:解析JSON对象以获取值(对象具有数组)

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值