如何过滤Android中JsonObject的值?

时间:2017-07-13 10:19:49

标签: java android json android-json

> {"-Kopv2EYUt7EeisRiiCz":{"deviceName":"LYF","fileUploadDate":"07\/12\/2017 2:00:57 PM"},
"-KopvA-cTtzgzSbsKTrw":{"deviceName":"LYF","fileUploadDate":"07\/12\/2017 2:01:29 PM",}}

如何从JsonObject解析所有“fileUploadDates”?

fileUploadDate = 07\/12\/2017 2:00:57 PM
fileUploadDate = "07\/12\/2017 2:01:29 PM

3 个答案:

答案 0 :(得分:2)

使用GSON库

public void parseJson(String json){
        Gson gson = new Gson();
        Type stringStringMap = new TypeToken<Map<String, Map<String,String>>>(){}.getType();
        Map<String,Map<String,String>> map = gson.fromJson(json, stringStringMap);

        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            Map<String,String> innerMap = (Map<String,String>)pair.getValue();
            getInnerMapDetail(innerMap);
            it.remove(); // avoids a ConcurrentModificationException
        }
}
    public void getInnerMapDetail(Map<String,String> innerMap)
    {   
         String fileUploadDate = innerMap.get("fileUploadDate"); // you will get here
        }

    }

答案 1 :(得分:0)

请尝试使用您的Parse

可以帮助你

try{
     JSONObject jsonObject = new JSONObject(response);
     List<String> keyList = getAllKeys(jsonObject);
     for(String key : keyList){

          JSONObject innerObject = jsonObject.getJSONObject(key);
          String deviceName = innerObject.getString("deviceName");
          String fileUploadDate = innerObject.getString("fileUploadDate");

          System.out.println(deviceName +"--"+ fileUploadDate);
      }
}catch(Exception e){
            e.printStackTrace();
}

此方法将返回键

private List<String> getAllKeys(JSONObject jsonObject) throws JSONException{
    List<String> keys = new ArrayList<String>();

    Iterator<?> iterator = jsonObject.keys();
    while( iterator.hasNext() ) {
        String key = (String)iterator.next();
        keys.add(key);
    }
   return keys;
}

答案 2 :(得分:-1)

首先,您需要解析json对象并将其存储到String中,就像那样

String fileUploadDate;
JSONObject -Kopv2EYUt7EeisRiiCz = new JSONObject;
fileUploadDate= sys.getString("fileUploadDate");

更改字符串之后

String newfileUploadDate = string.replace("\", "");

你挑衅地来输出。