1
我想將字符串轉換爲json數組並對其進行迭代。將字符串轉換爲json數組與GSON
String name = "lokesh";
String response = "[{"name":"lokesh"}, {"name":"cherukuri"}]";
JsonArray jsonArray = gson.fromJson(response, JsonArray.class);
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
System.out.println(jsonObject.get("name"));
if (jsonObject.get("name").toString().equals(name)) {
System.out.println("equal");
}
}
問題:由於引號引起的循環內If條件不成立。因爲這條線
System.out.println(jsonObject.get("name")); // printed "lokesh"
and System.out.println(name); //printed lokesh
我是否以錯誤的方式使用GSON?