都重复了
本来的 xml解析 // xml解析信息
public static Vector<CouponDetail> fromXmlToAdroundInfo(Element root) {
System.out.println(1);
Vector<CouponDetail> list = new Vector<CouponDetail>(50);
NodeList nlist = root.getElementsByTagName("result");
for (int i = 0; i < nlist.getLength(); i++) {
list.add(fromNode(nlist.item(i)));
// System.out.println("add"+fromNode(nlist.item(i)).getAddress()+"街店名字"+fromNode(nlist.item(i)).getShopname()+"街店坐标"+fromNode(nlist.item(i)).x+":"+fromNode(nlist.item(i)).y);
}
// System.out.println("list"+list.toString());
return list;
}
private static CouponDetail fromNode(Node n) {
CouponDetail ad = new CouponDetail();
try {
Class cls = ad.getClass();
for (int i = 0; i < n.getChildNodes().getLength(); i++) {
String fieldname = n.getChildNodes().item(i).getNodeName();
Field field = cls.getDeclaredField(fieldname);
field.set(ad, n.getChildNodes().item(i).getFirstChild()
.getNodeValue());
// System.out.println("field"+field.toString());
}
} catch (Exception e) {
// e.printStackTrace();
}
return ad;
}
修改后的解析 public static Vector<CouponDetail> JsonParseAround(String string) {
// System.out.println(string);
Vector<CouponDetail> list = new Vector<CouponDetail>();
try {
JSONObject object = new JSONObject(string);
count = object.getString("count");
type = object.getString("type");
JSONArray jsonArray = (JSONArray) object.get("nearby");
CouponDetail couponDetail = new CouponDetail();
for (int i = 0; i < jsonArray.length(); i++) {
// 解析对象
JSONObject detailObject = jsonArray.getJSONObject(i);
couponDetail.id = detailObject.getString("id");
couponDetail.title = detailObject.getString("title");
System.out.println(couponDetail.title);
couponDetail.address = detailObject.getString("address");
couponDetail.telno = detailObject.getString("telephone");
couponDetail.imgurl = detailObject.getString("imgurl");
couponDetail.distance = detailObject.getString("distance");
// 此处distance是1米,包含“米”这个字
couponDetail.discountType = detailObject
.getString("discountType");
JSONObject location = detailObject.getJSONObject("location");
couponDetail.x = location.getString("x");
couponDetail.y = location.getString("y");
list.add(couponDetail);
}
} catch (JSONException e) {
System.out.println("---");
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
确认解析没问题
------解决方案--------------------
CouponDetail couponDetail = new CouponDetail();
for (int i = 0; i < jsonArray.length(); i++) {
............
list.add(couponDetail);
}
看出问题了吗??