Java JSONObject获取子项 [英] Java JSONObject get children

查看:1345
本文介绍了Java JSONObject获取子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上创建gmaps。



我找到了,如何获取坐标。

  {
results:[
{
// body
formatted_address:Puławska,Piaseczno,Polska,
geometry:{
bounds:{
northeast:{
lat:52.0979041,
lng:21.0293984
},
西南:{
lat:52.0749265,
lng:21.0145743
}
},
location:{
lat:52.0860667,
lng:21.0205308
},
location_type:GEOMETRIC_CENTER,
viewport:{
northeast :{
lat:52.0979041,
lng:21.0293984
},
s outhwest:{
lat:52.0749265,
lng:21.0145743
}
}
},
partial_match:true,
类型:[路线]
}
],
状态:确定
}

我想得到:

 location :{
lat:52.0860667,
lng:21.0205308
},

来自这个Json。



我决定使用json-simple

 <依赖性> 
< groupId> com.googlecode.json-simple< / groupId>
< artifactId> json-simple< / artifactId>
< version> 1.1< / version>
< / dependency>

我的代码是:

 字符串coordinates = //来自google的JSON 

JSONObject json =(JSONObject)new JSONParser()。parse(coordinates);

我可以得到第一个孩子:结果

  String json2 = json.get(results)。toString(); 

json2显示正确的结果正文



<但是我不能得到位置孩子。



如何做?



谢谢

解决方案

我认为您需要将调用转发给 json.get(results) JSONArray



 字符串coordinates = //来自google的JSON 

JSONObject json =(JSONObject)new JSONParser()。parse(coordinates);

JSONArray results =(JSONArray)json.get(results);

JSONObject resultObject =(JSONObject)results.get(0);

JSONObject location =(JSONObject)resultObject.get(location);

String lat = location.get(lat)。toString();

String lng = location.get(lng)。toString()

诀窍是查看你要反序列化的json部分的类型,并将其转换为适当的类型。


i want to create gmaps on my website.

I found, how to get coordinates.

    {
   "results" : [
      {
         // body
         "formatted_address" : "Puławska, Piaseczno, Polska",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 52.0979041,
                  "lng" : 21.0293984
               },
               "southwest" : {
                  "lat" : 52.0749265,
                  "lng" : 21.0145743
               }
            },
            "location" : {
               "lat" : 52.0860667,
               "lng" : 21.0205308
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 52.0979041,
                  "lng" : 21.0293984
               },
               "southwest" : {
                  "lat" : 52.0749265,
                  "lng" : 21.0145743
               }
            }
         },
         "partial_match" : true,
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}

I want to get:

 "location" : {
           "lat" : 52.0860667,
           "lng" : 21.0205308
        },

From this Json.

I decided to user json-simple

<dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>

My code is:

String coordinates = //JSON from google

JSONObject json = (JSONObject)new JSONParser().parse(coordinates);

And i can get the first children: "results"

String json2 = json.get("results").toString();

json2 display correct body of "results"

But i cannot get "location" children.

How to do it ?

Thanks

解决方案

I think you need to cast the call to json.get("results") to a JSONArray.

I.e.

String coordinates = //JSON from google

JSONObject json = (JSONObject)new JSONParser().parse(coordinates);

JSONArray results = (JSONArray) json.get("results");

JSONObject resultObject = (JSONObject) results.get(0);

JSONObject location = (JSONObject) resultObject.get("location");

String lat = location.get("lat").toString();

String lng = location.get("lng").toString()

The trick is to look at what type the part of the json you're deserializing is and cast it to the appropriate type.

这篇关于Java JSONObject获取子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆