我有一个数据模型,那是一个String []。当我尝试使用以下代码将模型转换为JSONObject:将字符串数组作为字段转换为JSONObject
public class CheckList {
private String id = "123";
private String Q1 = "This is Question 1";
private String[] Q2 = ["Part 1", Part 2"];
public CheckList (String, id, String Q1, String[] Q2){
...
}
}
CheckList checklist = new Checklist("123", "This is Question 1", ["Part 1", "Part 2"]
JSONObject test = new JSONObject(checklist);
String []未被正确转换。通过上面的代码,我想一个JSONObject是这样的:
{
id: 123,
Q1: This is Question 1,
Q2: [Part 1, Part 2]
}
,但我得到的JSONObject是这样的:
{
id: 123,
Q1: This is Question 1,
Q2: [{"bytes":[{},{},{},{}],"empty":false},{"bytes":[{},{},{},{}],"empty":false}]
}
有没有什么办法来解决这个问题?提前致谢。
嘿,谢谢你的解决方案。我正在寻找更多的解决方案,将直接将模型转换为json。我的实际清单模型有20个字段,所以你的解决方案不适合我的用例。 – SL07