我正在开发一个使用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的新手,请帮我解决这个问题。
感谢你
答案 0 :(得分:2)
String point[] = jsonObject.getJSONObject("source").getString("point").split(",");
double lat = Double.parseDouble(point[0]);
double lon = Double.parseDouble(point[1]);
应该让你获得积分。
如果您正在寻找其他活动,请将它们放入Bundle
并将其传递给下一个活动。 Here是一篇解释如何传递捆绑包的帖子。
答案 1 :(得分:0)
尝试解串器。我使用谷歌GSON库来解决我遇到的类似问题。看看我如何解析json对象中的heroes
数组。
public class GameDeserializer implements JsonDeserializer<Game> {
public Game deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject().get("game").getAsJsonObject();
String id = obj.get("id").getAsString();
Integer turn = obj.get("turn").getAsInt();
Integer maxTurns = obj.get("maxTurns").getAsInt();
Hero[] heroes = new Hero[4];
for (int i = 0; i < 4; i++) {
heroes[i] = context.deserialize(obj.get("heroes").getAsJsonArray().get(i), Hero.class);
}
Board board = context.deserialize(obj.get("board"), Board.class);
Hero player = context.deserialize(json.getAsJsonObject().get("hero"), Hero.class);
String token = json.getAsJsonObject().get("token").getAsString();
String viewUrl = json.getAsJsonObject().get("viewUrl").getAsString();
String playUrl = json.getAsJsonObject().get("playUrl").getAsString();
boolean finished = obj.get("finished").getAsBoolean();
return new Game(id, turn, maxTurns, heroes, board, player, token, viewUrl, playUrl, finished);
}
}
}
我不会回答,但你的问题很模糊,我不确定你是否需要json或String的帮助。在后一种情况下,无视我的回答。