使用jackson Android从assets文件夹解析巨大的json(28MB)文件

时间:2018-01-30 15:17:25

标签: android

我是新的在android.I想解析来自巨大的Json(28 MB)文件的数据。我已经尝试但没有成功。我正在开发天气应用程序。其中国家和城市需要。我已经在openWeather Api中找到json文件并获取数据。但是文件太大了我想管理CountryWise城市所以我是试图只获取城市名称和国家,并合并国家明智的城市但我没有阅读文件。我已经尝试了很多东西来解决问题。请帮助我学习新事物。

这是我的主要活动

public class MainActivity extends AppCompatActivity {

    Map<String,String> CountryListHash ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        (findViewById(R.id.B1)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream is = null;
                BufferedReader br=null;
                ObjectMapper mapper;
                City jsoncity;
                CountryListHash = new TreeMap<>();
                 StringBuffer json;
                    try {
                   br = new BufferedReader(new InputStreamReader(getAssets().open("city.list.json"), "UTF-8"));
                        json = new StringBuffer();
                        String tmp;
                        while ((tmp = br.readLine()) != null) {
                            json.append(tmp);
                            mapper = new ObjectMapper();
                            jsoncity  = mapper.readValue(json.toString(),City.class);
                            CountryListHash.put( jsoncity.getCountry(),(jsoncity.getCity()));
                            Log.e("City",  ""  + jsoncity.getCountry() +" " +jsoncity.getCity());
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    finally {
                        if (br != null) {
                            try {
                                br.close();
                            } catch (IOException e) {
                                //log the exception
                            }
                        }
                    }


            }
        });
    }

这是我的城市课程

@JsonIgnoreProperties(ignoreUnknown=true)

public class City {


 private String name;

 private String country;

 public City(){}

 public String setCity(String name){
   return  this.name=name;
 }

 public String setCountry(String country){
      return this.country=country;
 }



  public String getCity(){
     return name;
  }

   public String getCountry(){
      return country;
   }

}

这是我的JSon文件

[
  {
    "id": 707860,
    "name": "Hurzuf",
    "country": "UA",
    "coord": {
      "lon": 34.283333,
      "lat": 44.549999
    }
  },
  {
    "id": 519188,
    "name": "Novinki",
    "country": "RU",
    "coord": {
      "lon": 37.666668,
      "lat": 55.683334
    }
  },
...And Many More

1 个答案:

答案 0 :(得分:0)

据我所知,onClick回调正在UI线程上运行,因此您不应在UI线程上执行任何IO操作。尝试创建一个新线程,并将onClick回调中的所有操作移至新线程。希望它能对您有所帮助。