android 用gson解析遍历json数据

时间:2023-02-03 10:25:00

jsonString数据如:{"id":"10","name":"chenobj1","type":"5"},在android中如何用gson遍历呢?

方法1:

    Gson gson=new Gson();
Type type=new TypeToken<Map<String,String>>(){}.getType();
Map<String,String> map=gson.fromJson(jsonString,type);
for (Map.Entry<String,String> entry : map.entrySet()){
Log.v(TAG, entry.getKey()+"|"+entry.getValue());
}


方法2:

    JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(jsonString).getAsJsonObject();
Iterator i$ = jsonObj.entrySet().iterator();
while(i$.hasNext()) {
Map.Entry entry = (Map.Entry)i$.next();
Log.v(TAG, entry.getKey()+"|"+entry.getValue());
}