我有一个类似于下面示例的 JSON,我正在尝试获取一些值,例如值。
results.shipper.id
{
"results": [
{
"updated": false,
"notification": false,
"some_data": {
"id": 15989,
"pieces": 0,
},
"shipper": {
"updated": false,
"notification": false,
"id": 1587,
"parent": {
"updated": false
},
我试图通过这种方式获得价值:
String test = shipmentData.getJSONObject("shipper").getString("id");
但它总是抛出异常。我认为,该异常是由于我没有通过“结果”数组访问值而引起的。
我怎样才能轻松获得我需要的价值。
我尝试找到一些助手(Gson、fast-json 等),但使用起来似乎相当复杂(我想使用 JSON 树来直接访问“即时”值或访问值就像对象一样,这意味着.. Object.InnerObject.value )。
所以问题是我怎样才能做对呢?
感谢您的任何建议。
需要遍历JSON才能访问id
:
JSONArray results = shipmentData.getJSONArray("results");
JSONObject first = results.getJSONObject(0);
JSONObject shipper = first.getJSONObject("shipper");
Integer id = shipper.getInt("id");
将 int 解析为字符串:
String id = String.valueOf(shipper.getInt("id"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)