你有什麼是JSON與一些分號分隔的字符串中。我根本不會稱這爲CSV格式。
您可以使用像Gson這樣的JSON解析器來解析JSON到Java對象,但是您仍然需要從Java對象中選擇「列」,因爲它們沒有在JSON中正確定義。
像這樣的東西應該工作,我建議你添加更多的錯誤檢查比我雖然:
public class DBEntry {
@SerializedName("ParkingSpaces;;;;")
@Expose
private String ParkingSpaces;
public String getParkingSpaces() {
return ParkingSpaces;
}
public void setParkingSpaces(String ParkingSpaces) {
this.ParkingSpaces = ParkingSpaces;
}
}
public static void main(String[] args) {
String json = "[{\"ParkingSpaces;;;;\":\"Name;CarPlate;Description;ExpirationDate;Owner\"},{\"ParkingSpaces;;;;\":\"A5;T555;Parkingspace A5;;\"},{\"ParkingSpaces;;;;\":\"A6;T666;Parkingspace A6;;\"},{\"ParkingSpaces;;;;\":\"A7;T777;Parkingspace A7;;\"},{\"ParkingSpaces;;;;\":\"\"}]";
// Convert JSON to java objects using the popular Gson library
Gson gson = new Gson();
Type collectionType = new TypeToken<ArrayList<DBEntry>>(){}.getType();
List<DBEntry> results = gson.fromJson(json, collectionType);
boolean header = true;
for (DBEntry result : results) {
// Ignore the header and empty rows
if (header || result.getParkingSpaces().isEmpty()) { header = false; continue; }
// Grab the columns from the parking spaces string
String[] columns = result.getParkingSpaces().split(";");
// TODO: Store this record in your database
System.out.println("New entry: " + StringUtils.join(columns, ", "));
}
}
請http://stackoverflow.com/questions/15246030/json-string-to-java-對象 – Alice
'{「ParkingSpaces ;;;;」:「Name; CarPlate; Description; ExpirationDate; Owner」}'是你的JSON字符串嗎? – Alice
@Alice是的,這是我用angular-csv-import庫轉換後得到的。 –