网络服务:
http://maps.google.com/maps/api/geocode/json?address=pune+India&sensor=false
继Json之后我得到了:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "Maharashtra",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Pune, Maharashtra, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.62719520,
"lng" : 73.98963809999999
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.75319080
}
},
"location" : {
"lat" : 18.52043030,
"lng" : 73.85674370
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.60505030,
"lng" : 73.98480309999999
},
"southwest" : {
"lat" : 18.43576840,
"lng" : 73.72868430
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
代码:
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.pxr.tutorial.xmltest.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://maps.google.com/maps/api/geocode/json?address=pune+India&sensor=false");
try{
JSONArray earthquakes = json.getJSONArray("location");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
//map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("lat"));
map.put("magnitude", "Magnitude: " + e.getString("lng"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "magnitude" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
任何人都可以帮我解析android中的lat和long的Json。感谢
答案 0 :(得分:0)
我认为您在理解答案的JSON模式时遇到问题,
我希望我理解正确并写下来,
JSONObject mainObj= new JSONObject(response);
JSONArray jArray = mainObj.optJSONArray("results");
for(int i=0;i<jArray.length();i++)
{
JSONObject temp = jArray.optJSONObject(i);
JSONObject loc = temp.optJSONObject("geomatery").optJSONObject("location");
loc.getString("lat");
loc.getString("lng");
}