【发布时间】:2014-05-12 15:49:18
【问题描述】:
我正在开发一个使用 JSON 响应的应用程序,它同时具有 JSONObject 和 JSONArray。这两种信息都是在地图上放置标记所必需的。但我在解析两者时都面临问题。
这是我打电话给我的 URl 时的回应
{
-source: {
LS: " ABCDEF",
name: "XYXA",
point: "77.583859,12.928751"
},
-stores: [
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
},
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
}
]
}
这就是我尝试处理 JSON 响应的方式
public class JSONResponseHandler implements ResponseHandler<List<LatlongRec>> {
@Override
public List<LatlongRec> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
List<LatlongRec> result = new ArrayList<LatlongRec>();
String JSONResponse = new BasicResponseHandler()
.handleResponse(response);
try {
JSONObject object = (JSONObject)new JSONTokener(JSONResponse)
.nextValue();
JSONArray stores = object.getJSONArray("stores");
for (int i = 0; i < stores.length(); i++) {
JSONObject tmp = (JSONObject) stores.get(i);
result.add(new LatlongRec(tmp.getString("point"), tmp
.getString("name"), tmp.getString("LS"), tmp
.getString("ph")));
Log.v("points", "" + tmp.getString("point"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}
在这个 LatlongRec 是一个我创建了构造函数和 getter 的类。
这是我的主要活动
public class SecondActivity extends Activity {
private static final double CAMERA_LNG = 77.583859;
private static final double CAMERA_LAT = 12.928751;
// The Map Object
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String city = getIntent().getStringExtra("city");
String area1 = getIntent().getStringExtra("area");
String area = area1.trim();
String URL = "http://xyzxyz.search.json?query="
+ area + "&city=" + city + "&app_id=test";
new HttpGetTask().execute(URL);
}
private class HttpGetTask extends AsyncTask<String, Void, List<LatlongRec>> {
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected List<LatlongRec> doInBackground(String... params) {
HttpGet request = new HttpGet(params[0]);
JSONResponseHandler responseHandler = new JSONResponseHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
@SuppressLint("NewApi")
protected void onPostExecute(List<LatlongRec> result) {
// Get Map Object
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
if (null != mMap) {
// Add a marker for every earthquake
for (final LatlongRec rec : result) {
Log.v("pointM", "" + rec.getPoint());
Log.v("NAMES", "" + rec.getName());
String point = rec.getPoint();
String[] latlng = point.split(",");
String lat = latlng[0];
String lng = latlng[1];
Double lat1 = Double.parseDouble(lat);
Double lng1 = Double.parseDouble(lng);
Log.v("LATLNGM", "lat" + lng + "& lng " + lat);
// Add a new marker for each point
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lng1, lat1))
.title(rec.getName())
.snippet(rec.getLS() + rec.getPh())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.main_marker)));
// Setting a custom info window adapter for the google map
CameraPosition cp = new CameraPosition(new LatLng(CAMERA_LAT,
CAMERA_LNG), 15, 40, 90);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}
if (null != mClient)
mClient.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
如何将源对象中的点获取到我的主要活动,以便我可以从该点将 CAMERA_LAT 和 CAMERA_LNG 设置为 Lat 和 Lng。?
我是 JSON 新手,请帮我解决这个问题。
谢谢你
【问题讨论】:
-
你的结果是一个json对象。
-
@njzk2 你能告诉我如何将“源”对象指向主要活动吗?
-
@dharshan 乍一看这似乎不是一个 json 问题。您的意图是将点分为纬度和经度值吗?
-
哼,琐碎
object.getJSONObject("source")?