[英]Extracting data from JSON using GSON in java
Help please, this is my first time using JSON/GSON. 请帮助,这是我第一次使用JSON / GSON。 I've got this JSON data from google geocoding API and can't figure out how to extract "formatted_address" from it. 我已经从Google地理编码API中获得了此JSON数据,无法弄清楚如何从中提取“ formatted_address”。
{
"results" : [
{
"address_components" : [
{
"long_name" : "Google Building 41",
"short_name" : "Google Bldg 41",
"types" : [ "premise" ]
},
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.4228642,
"lng" : -122.0851557
},
"southwest" : {
"lat" : 37.4221145,
"lng" : -122.0859841
}
},
"location" : {
"lat" : 37.4224082,
"lng" : -122.0856086
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238383302915,
"lng" : -122.0842209197085
},
"southwest" : {
"lat" : 37.4211403697085,
"lng" : -122.0869188802915
}
}
},
"place_id" : "ChIJxQvW8wK6j4AR3ukttGy3w2s",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
This is what I've tried but it throws IndexOutOfBoundsException: 这是我尝试过的方法,但它抛出IndexOutOfBoundsException:
public String getLocationData(String location) {
try {
URL url = new URL(location);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
JsonParser parser = new JsonParser();
JsonElement content = parser.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject jObject = content.getAsJsonObject();
JsonArray jArray = jObject.getAsJsonArray("results");
jObject = jArray.get(1).getAsJsonObject();
String result = jObject.getAsString();
return result;
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return "fail";
}
You wrote jObject = jArray.get(1).getAsJsonObject();
您编写了jObject = jArray.get(1).getAsJsonObject();
, but your array only contains one element. ,但您的数组仅包含一个元素。 Remember that arrays are 0-indexed in Java. 请记住,在Java中数组是0索引的。 You should use jObject = jArray.get(0).getAsJsonObject();
您应该使用jObject = jArray.get(0).getAsJsonObject();
instead. 代替。
I can only hazard a guess as to what your immediate error is without a stacktrace, but you're trying to access the second item of an array that only contains one item : 我只能冒险猜测没有栈跟踪的立即错误是什么,但是您正在尝试访问仅包含一项的数组的第二项:
the results
array contains a single element (an object whose fields are the address_components
array, formatted_address
, geometry
, place_id
and types
) results
数组包含一个元素(一个对象,其字段为address_components
数组, formatted_address
, geometry
, place_id
和types
)
you're trying to access its second element with jArray.get(1)
(arrays in Java are 0-indexed) 您正在尝试使用jArray.get(1)
访问其第二个元素(Java中的数组为0索引)
This is a typical cause of IndexOutOfBoundsException
. 这是IndexOutOfBoundsException
的典型原因。
You probably want to access the only item with jArray.get(0)
which you will be able to manipulate as a JsonObject
whose fields you will be able to access. 您可能希望使用jArray.get(0)
访问唯一的项目, jArray.get(0)
能够作为JsonObject
进行操作,并且可以访问其字段。
//URL url = new URL("http://localhost:7001/JMR_SERVICE/services/CalSerice?wsdl");
URL endPointUrl= new URL("http://192.168.0.161:7200/JMR_SERVICE/services/CalSerice?wsdl");
org.apache.axis.client.Service service=new org.apache.axis.client.Service();
CalSericeSoapBindingStub stub=new CalSericeSoapBindingStub( endPointUrl,service);
String msg=stub.hello("hello how are you..............?");
String resp=stub.getID("ADMINUSER4");
public class Dao {
String result="";
public String getDetails(int empid)
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","om");
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("connected data base");
try
{
//stmt.execute("create table com (sno int,name varchar(90),age int)");
System.out.println("connection is established");
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
PreparedStatement psmt=con.prepareStatement("select name from emp122 where id=id");
//psmt.setInt(2,id);
String query="select name from emp120 where id='emid'";
ResultSet rs=psmt.executeQuery();
//String result="";
while(rs.next())
{
result=rs.getString(1);
}
System.out.println(result);
}
catch(Exception ex)
{
ex.printStackTrace();
return "Not Record found";
}
return result;
}
public ArrayList<String[]> getconnection(String id)throws ClassNotFoundException, SQLException
{
System.out.println("collection jdbc");
ArrayList ls=new ArrayList();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@192.168.1.92:1521:FCARB01","EODMS","eodms");
System.out.println("connected data base");
Statement stmt=con.createStatement();
String sql="select QRY_TEXT from EOD_QRY_EXEC_LOG where OPERATOR_ID ='"+id+"'";
System.out.println("fetching value....");
ResultSet rs=stmt.executeQuery(sql);
System.out.println("resulsetmetadata");
ResultSetMetaData rsmd=rs.getMetaData();
int a=rsmd.getColumnCount();
while(rs.next())
{
for(int i=1;i<=a;i++)
{
ls.add(rs.getString(i));
}
}
return ls;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.