在java中将JSON字符串转换为自定义对象

在java中将JSON字符串转换为自定义对象,java,json,Java,Json,我正在尝试将一个字符串转换为以下字符串,以便从中获取属性。我试图通过获取它们的属性并从中生成对象来将它们插入数据库中 [{“Parkingspace;”;;;;;;;;;:“名称;停车板;描述;到期日期;所有者”},{“Parkingspace;”;;;;;;;;;;;;;;;;;;;;;;;:“A5;T555;Parkingspace A5 我从CSV文件中得到了这个字符串 有谁知道我该怎么做吗 提前感谢。您的代码非常混乱,但它是可行的。您可以使用示例中的方法: final String j

我正在尝试将一个字符串转换为以下字符串,以便从中获取属性。我试图通过获取它们的属性并从中生成对象来将它们插入数据库中

[{“Parkingspace;”;;;;;;;;;:“名称;停车板;描述;到期日期;所有者”},{“Parkingspace;”;;;;;;;;;;;;;;;;;;;;;;;:“A5;T555;Parkingspace A5

我从CSV文件中得到了这个字符串

有谁知道我该怎么做吗


提前感谢。

您的代码非常混乱,但它是可行的。您可以使用示例中的方法:

 final String json = "[{\"ParkingSpaces;;;;\":\"Name;CarPlate;Description;ExpirationDate;Owner\","
{\"ParkingSpaces;;;;\":\"A5;T555;Parkingspace A5;;\"},{\"ParkingSpaces;;;;\":\"A6;T666;Parkingspace A6;;\"},{\"ParkingSpaces;;;;\":\"A7;T777;Parkingspace A7;;\"},{\"ParkingSpaces;;;;\":\"\"}]";
        final org.json.JSONArray jSONArray = new JSONArray(json);
        for (int i = 0; i < jSONArray.length(); i++) {
            final org.json.JSONObject jSONObject = jSONArray.getJSONObject(i);
            final String parkingSpaces = jSONObject.getString("ParkingSpaces;;;;");
            final String spaces[] = parkingSpaces.split(";");
            System.out.println(Arrays.toString(spaces));
        }
    }
final String json=“[{\”Parkingspace;;;;;\”:\“名称;CarPlate;说明;过期日期;所有者\”,”
{\'停车空间;;;;;;;;:\'A5;T555;停车空间A5;;;;;;,{\'停车空间;;;;;;;;;;;;;;;;;:\'A6;T666;停车空间A6;;;;;;;;,,{\'停车空间;;;;;;;:;
final org.json.JSONArray JSONArray=新的JSONArray(json);
for(int i=0;i

或者。

你有的是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, ", "));
    }
}
公共类DBEntry{
@SerializedName(“Parkingspace;;;”)
@暴露
私人停车场;
公共字符串getParkingSpace(){
返回停车场;
}
公共无效设置ParkingSpace(字符串ParkingSpace){
this.parkingspace=停车空间;
}
}
公共静态void main(字符串[]args){
字符串json=“[{\”停车空间;;;;;;\”:\”名称;停车板;描述;到期日期;所有者\“},{\”停车空间;;;;;;;;;\”:\”A5;T555;停车空间A5;;;;;;;;;;;“:”A6;T666;停车空间A6;;;;;;;;;;;;;;;;;“:”A7;T777;停车空间A7;
//使用流行的Gson库将JSON转换为java对象
Gson Gson=新的Gson();
Type collectionType=new-TypeToken(){}.getType();
List results=gson.fromJson(json,collectionType);
布尔头=真;
for(数据库条目结果:结果){
//忽略标题和空行
if(header | | result.getparkingspace().isEmpty()){header=false;continue;}
//从停车位字符串中抓取列
String[]columns=result.getParkingSpace().split(;);
//TODO:将此记录存储在数据库中
System.out.println(“新条目:“+StringUtils.join(columns)”,”);
}
}

请检查
{“ParkingSpaces;;;;;;:“名称;车板;说明;过期日期;所有者”}
这是你的JSON字符串吗?@Alice是的,这是我用angular csv导入库转换后得到的。这不是csv格式,它是包含分号分隔字符串的JSON。使用这段代码后,我可以做我想做的事,谢谢。