以JSON格式从URL本身解析内容

时间:2014-02-19 07:40:06

标签: android json parsing url

URL:的“http://xyz/index.php/api/Users/滤波器= [{ ”属性“?: ”vcUserName“, ”值“: ”QWERTY“,”操作员“:”=“}]'

任何人都可以请我解析这种来自JSON格式的网址

的数据

1 个答案:

答案 0 :(得分:2)

您发送HTTP请求并获取响应,即查询结果。获得“结果”后,您可以创建JSONObject:

JSONObject jObject = new JSONObject(result);

使用此jObject使用特定名称获取不同类型:

字符串:

String jsonString = jObject.getString("NAME");

阵列:

JSONArray jsonArray = jObject.getJSONArray("NAME");

EG。为了解析place结果,我使用了以下函数,其中JSON类似于:

  "results" : [
 {
 "geometry" : {
    "location" : {
       "lat" : 36.817729,
       "lng" : 10.18206
    }
 },
 "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
 "id" : "35a4f720fff88e2cfb94d64bfd7dbe95a8b4a632",
 "name" : "Banque Al-Baraka",
 "reference" : "CoQBcwAAAJbocdTKVg8I8CzgDdNtBQQaMsaghRlks-IWYl9eDNmFtftPHMgEfVeek_NHJZ2AN9JbiMda1WvREmoeIHBHsNdz9i7gtBaLM1xB93uhema_oswpqD-eRQ9b3fvTo4MhTOeIa7cRJ70BSEtDMONZqFyjqlGvL-5WsiwmqI1F3Vp_EhArWzyFuNrJdly2cHRBNxUJGhQjlcyHs-U2F0ILpN-ce-PHEesdqA",
 "types" : [ "bank", "finance", "establishment" ],
 "vicinity" : "88 P9, Tunis"
 },
 {
 "geometry" : {
    "location" : {
       "lat" : 36.861635,
       "lng" : 10.164628
    }
 },
 "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
 "id" : "da42093835270f902f64bc870154e6787fffbc7b",
 "name" : "Agence Ennasr",
 "reference" : "CnRwAAAAoh-XuMXghUKtv2UtHQJiRXB0ZugUnAkJIyP-vBd2YzTj5GdwIb5XhUS9x5-uY9_OwyQUdMmUeDHYm4KXHFNvOoj7diOOHAGBu-xCI4svIxcCwQ2w063mc2G3lohiScYMNpbKhwnCaggt0H1iknZY6xIQxDvNwVN0wWkVwO8zf_El5hoUwHgmgFflE31LDLj2_rr0uc1zvZM",
 "types" : [ "bank", "finance", "establishment" ],
 "vicinity" : "Avenue de Hédi Nouira, Ariana"
 },
  ]

解析JSON数组

  public ArrayList<HashMap<String,String>> parseResult(JSONObject jsonObject) throws JSONException         {        
     JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESULTS);
     ArrayList<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
     for (int i = 0; i < jsonArray.length(); i++) {
        Object object = jsonArray.get(i);
        if (object instanceof JSONObject) {
            placesList.add(parsePlaceInfo((JSONObject) object));
        }
     }
    return placesList;

}

解析简单的JSON对象

private HashMap<String, String> parsePlaceInfo(JSONObject place) throws JSONException {
    String name = place.getString(Constants.NAME);
    String icon = place.getString(Constants.ICON);
    String vicinity = place.getString(Constants.VICINITY);
    JSONObject locationObject = place.getJSONObject(Constants.GEOMETRY).getJSONObject(Constants.LOCATION);
    String lat = locationObject.getString(Constants.LAT);
    String lng = locationObject.getString(Constants.LNG);
    HashMap<String, String> placeDetails = new HashMap<String, String>();
    placeDetails.put(Constants.NAME, name);
    placeDetails.put(Constants.ICON, icon);
    placeDetails.put(Constants.VICINITY, vicinity);
    placeDetails.put(Constants.LAT, lat);
    placeDetails.put(Constants.LNG, lng);

   return placeDetails;
}

请在此处查看我的答案,以获取有关发出HTTP请求和解析响应的更多信息:

https://stackoverflow.com/questions/21872643/about-json-parsing-exchange-data-with-rest-services/21872688#21872688

网上有一个很好的JSON解析tutorial

如果您需要任何具体细节,请与我们联系。谢谢!