我有一個JSONArray
,看起來像這樣:JSONArray爲String在Java中
我怎樣才能將其轉換成字符串?
,如果我嘗試:
json.toString();
字符串爲:
["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"]
,但我想是這樣的:
{
"json":
"values": [
{
"answer": "true",
"remark":"",
"questionId": "0"
"checklistId": "2"
},
{
"answer": "true",
"remark":"",
"questionId": "0"
"checklistId": "2"
}
]
}
編輯:
這個剪斷如何我使json數組:
if(cb.isChecked() || !text.getText().toString().equals("")){
ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(), text.getText().toString());
answers.add(answer);
}
}
JSONArray json = new JSONArray(answers);
String jsonString = json.toString();
顯示你的代碼 – 2014-11-06 09:51:51
它按預期工作......你只要把'的一些片斷ChecklistAnswer'對象到數組......不是'JSONObject' ...'ChecklistAnswer.toString()'返回「[email protected]」...所以要麼覆蓋ChecklistAnswer.toString方法返回json字符串,要麼創建函數將ChecklistAnswer轉換爲JSOObject – Selvin 2014-11-06 09:51:52
顯示代碼用於json解析 – 2014-11-06 09:54:20