java接收jsonarray,Java-如何获取JSON数组中的对象值?

i have a JSON like example below and i'm trying to get some values, for example value of.

results.shipper.id

{

"results": [

{

"updated": false,

"notification": false,

"some_data": {

"id": 15989,

"pieces": 0,

},

"shipper": {

"updated": false,

"notification": false,

"id": 1587,

"parent": {

"updated": false

},

I'm trying to get value by this way:

String test = shipmentData.getJSONObject("shipper").getString("id");

But it always throws a exception. I think, that exception is caused because of the i am not accessing to the values via "results" array.

How can i easy access to the value what i need.

I tried find some helpers (Gson, fast-json, etc..) but it seems to be a quite complicated for using (i would like to work with JSON tree for direct access to values "on-the-fly" or access to values like to a object, it means.. Object.InnerObject.value ).

So question is how can i do it right?

Thanks for any advice.

解决方案

JSON needs to be traversed in order to access id:

JSONArray results = shipmentData.getJSONArray("results");

JSONObject first = results.getJSONObject(0);

JSONObject shipper = first.getJSONObject("shipper");

Integer id = shipper.getInt("id");

Parse int to string:

String id = String.valueOf(shipper.getInt("id"));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值