• Android json解析


      在做项目时,从网络获取的数据是json格式,需要进行解析,服务器返回的数据是整体无序,局部有序的,一开始有点慌,因为只知道有序的解析方式,后来查看API,可以先将jsondata转化封装成JSONBObject,然后再把有序部分封装成JSONArray。

    jsondata如下:

    具体解析过程如下:

    private void parseJSONWithJSONObject(String jsondata){
    try {
    JSONObject jsonObject0 = new JSONObject(jsondata);
    JSONArray jsonArray = jsonObject0.getJSONArray("data");
    for(int i = 0; i < jsonArray.length(); i++)
    {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String cityId = jsonObject.getString("CityID");
    String cityName = jsonObject.getString("CityName");
    String spotId = jsonObject.getString("SpotID");
    String spotName = jsonObject.getString("SpotName");
    String spotPicUrl = jsonObject.getString("SpotPicUrl");
    int countGood = jsonObject.getInt("CountGood");
    int countLoveLocks = jsonObject.getInt("CountLoveLocks");
    int CountBless = jsonObject.getInt("CountBless");
    int CountWish = jsonObject.getInt("CountWish");

    Log.i("jsonToData cityId",cityId);
    Log.i("jsonToData cityName",cityName);
    Log.i("jsonToData spotName",spotName);
    Log.i("jsonToData spotPicUrl",spotPicUrl);
    Log.i("jsonToData countGood",""+countGood);
    Log.i("jsonToData countLoveLocks",""+countLoveLocks);
    Log.i("jsonToData CountBless",""+CountBless);
    }
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

  • 相关阅读:
    AtCoder Grand Contest 015 C
    AtCoder Grand Contest 011 F
    AtCoder Regular Contest 076 E
    AtCoder Grand Contest 016 B
    AtCoder Grand Contest 010 F
    AtCoder Regular Contest 081 F
    AtCoder Grand Contest 018 D
    AtCoder Regular Contest 076 F
    yii2.0 如何按需加载并管理CSS样式及JS脚本
    ebay商品基本属性组合成数据表格式,可用上传到系统递交数据
  • 原文地址:https://www.cnblogs.com/new-comer/p/5375176.html
Copyright © 2020-2023  润新知