Demo entry 6759926

java

   

Submitted by anonymous on Sep 05, 2018 at 14:15
Language: C++. Code size: 1.4 kB.

public static JsonObject obtainCityList(Context context) {  
    if (DEBUG) LOG.d(TAG, "obtainCityList");  
    JsonObject object = null;  
    try {  
        InputStreamReader inputStreamReader = new InputStreamReader(context.getAssets().open("maoyan_citylist.json"), "UTF-8");  
        BufferedReader reader = new BufferedReader(inputStreamReader);  
        object = new JsonParser().parse(reader).getAsJsonObject();  
        reader.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return object;  
}  
  
public static String obtainCityId(Context context, JsonObject object, String addressLine) {  
    if (DEBUG) LOG.d(TAG, "obtainCityId, addressLine = " + addressLine);  
    if (null == object || null == addressLine) return null;  
    JsonArray array = object.get("data").getAsJsonObject().getAsJsonArray("data");  
    if (null != array && array.size() > 0) {  
        if (DEBUG) LOG.d(TAG, "obtainCityId, array.size() = " + array.size());  
        for (int i = 0; i < array.size(); i++) {  
            String name = array.get(i).getAsJsonObject().get("name").getAsString();  
            String id = array.get(i).getAsJsonObject().get("id").getAsString();  
            if (addressLine.contains(name)) {  
                return id;  
            }  
        }  
    }  
    LOG.i(TAG, "obtainCityId, obtain city id failed, return null.");  
    return null;  
} 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).