Android JSON for JAVA语句
输出会不断重复每个属性的名称。只需查看一次名称,即可在单个名称下打印出每次事件的道具名称和地址。JSON看起来像:Android JSON for JAVA语句,java,android,json,parsing,Java,Android,Json,Parsing,输出会不断重复每个属性的名称。只需查看一次名称,即可在单个名称下打印出每次事件的道具名称和地址。JSON看起来像: [{"pmid":"2","name":"CARLYLE MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":"Bonneville Tower","address":"25801 Lakeshore","city":"Euclid","state":"OH","zip":"44132","lat"
[{"pmid":"2","name":"CARLYLE MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":"Bonneville Tower","address":"25801 Lakeshore","city":"Euclid","state":"OH","zip":"44132","lat":"41.6223","long":"-81.5034"}]},
{"pmid":"1","name":"COMMERCIAL ONE REALTY","result":"18","properties":[{"prop_id":"3","prop_name":"Autumn Chase Apartments","address":"146 Grand Circuit Blvd.","city":"Delaware","state":"OH","zip":"43015","lat":"40.3105","long":"-83.1138"},
{"prop_id":"6","prop_name":"Barrington Club Apartments","address":"4600 Barrington Club","city":"Columbus","state":"OH","zip":"43220","lat":"40.0536","long":"-83.0448"},{"prop_id":"17","prop_name":"Battleship Building Condominium","address":"444 North Front St.","city":"Columbus","state":"OH","zip":"43215","lat":"39.9712","long":"-83.0042"}]
代码当前为:
String pmid = mJsonObject.getString("pmid");
String name = mJsonObject.getString("name");
String result = mJsonObject.getString("result");
JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
int innerLength = mJsonArrayProperty.length();
for (int k = 0; k < innerLength; k++) {
// JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
// for (int i = 0; i < mJsonArrayProperty.length(); i++) {
JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(k);
String prop_id = mJsonObjectProperty.getString("prop_id");
String prop_name = mJsonObjectProperty.getString("prop_name");
String address = mJsonObjectProperty.getString("address");
String city = mJsonObjectProperty.getString("city");
String state = mJsonObjectProperty.getString("state");
String zip = mJsonObjectProperty.getString("zip");
String lat = mJsonObjectProperty.getString("lat");
String lon = mJsonObjectProperty.getString("long");
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_EMAIL, prop_name);
contact.put(TAG_NAME, name);
contact.put(TAG_PHONE, address);
// adding contact to contact list
contactList.add(contact);
}
} }catch (JSONException e) {
e.printStackTrace();
}
String pmid=mJsonObject.getString(“pmid”);
String name=mJsonObject.getString(“名称”);
字符串结果=mJsonObject.getString(“结果”);
JSONArray mJsonArrayProperty=mJsonObject.getJSONArray(“属性”);
int innerLength=mjProperty.length();
for(int k=0;kvalue
联系人。放置(标签、电子邮件、项目名称);
联系人:put(标签名称、姓名);
联系方式(电话、地址);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}}catch(JSONException e){
e、 printStackTrace();
}
输出给了我prop_的名称和地址,但每次都会反复显示名称。目标是查看一次名称,然后查看其下的多个属性。如何使用for或if语句或任何其他方式来实现这一点 我只是在写算法,你怎么做
- 首先,您必须获得
,其中包含JSONObject
jsondata
- 从该
中,您应该得到JSONObject
,因为您拥有的整个JSONArray
是一个数组jsondata
- 现在,从该
中,您应该获得字符串JSONArray
、名称等李>pmid
- 现在您必须再次获得
,因为JSONArray
是属性
,现在您可以迭代它了李>JSONArray
试试这个。。。
Try Out this...
JSONArray one=new JSONArray(json);
for (int i = 0; i < one.length(); i++)
{
JSONObject jsonObject=one.getJSONObject(i);
String pmid=jsonObject.getString("pmid");
String name=jsonObject.getString("name");
String result=jsonObject.getString("result");
JSONArray properties=jsonObject.getJSONArray("properties");
for (int j = 0; j < properties.length(); j++)
{
JSONObject js=properties.getJSONObject(j);
String prop_id=js.getString("prop_id");
String prop_name=js.getString("prop_name");
String address=js.getString("address");
String city=js.getString("city");
String state=js.getString("state");
String zip=js.getString("zip");
String lat=js.getString("lat");
String Long=js.getString("long");
}
}
JSONArray one=新JSONArray(json);
对于(int i=0;i<1.length();i++)
{
JSONObject JSONObject=1.getJSONObject(i);
字符串pmid=jsonObject.getString(“pmid”);
String name=jsonObject.getString(“名称”);
字符串结果=jsonObject.getString(“结果”);
JSONArray properties=jsonObject.getJSONArray(“属性”);
对于(int j=0;j
您不是在使用CustomAdapter
??您的mJsonObject
是一个数组吗?是的BBDev它是一个数组每次列出属性时仍会再次显示此名称