2014-11-06 68 views
0

我有一個JSONArray,看起來像這樣:JSONArray爲String在Java中

enter image description here

我怎樣才能將其轉換成字符串?

,如果我嘗試:

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(); 
+0

顯示你的代碼 – 2014-11-06 09:51:51

+1

它按預期工作......你只要把'的一些片斷ChecklistAnswer'對象到數組......不是'JSONObject' ...'ChecklistAnswer.toString()'返回「[email protected]」...所以要麼覆蓋ChecklistAnswer.toString方法返回json字符串,要麼創建函數將ChecklistAnswer轉換爲JSOObject – Selvin 2014-11-06 09:51:52

+0

顯示代碼用於json解析 – 2014-11-06 09:54:20

回答

0

你不需要使用任何額外的庫。 JSONArray班有an extra .toString(int) method,爲您打印漂亮的照片。 int參數是縮進因子。

JSONArray arr = ... 
System.out.println(arr.toString(4)); 

它也適用於JSONObject

您的大問題是您的JSONArray未正確構建。您應該添加其他JSONArray s和JSONObject s,但您要添加其他對象。它們隱含地變成了Strings。在將它們放入數組之前,您需要將它們轉換爲JSON。

+0

爲什麼downvote? – 2014-11-06 09:55:36

+0

對不起,但問題不在這裏... JSONArray.toString(int)== JSONArray.toString()與默認值(prolly不合格的所有) – Selvin 2014-11-06 09:55:43

+0

@Selvin看起來他有兩個問題!無論如何,我現在已經涵蓋了兩者。 – 2014-11-06 09:58:52

-1

試試這個:

JsonArray jArray = //Your Json Array; 
JSONObject jObj = new JSONObject(); 
jObj.put("test", jArray); 
String requiredString = jObj.optString("test"); 
0

的問題是不是JSONArray.toString(),如提及@Selvin。

從JSONArray來源:

/** 
* Encodes this array as a compact JSON string, such as: 
* <pre>[94043,90210]</pre> 
*/ 
@Override public String toString() { 
    try { 
     JSONStringer stringer = new JSONStringer(); 
     writeTo(stringer); 
     return stringer.toString(); 
    } catch (JSONException e) { 
     return null; 
    } 
} 

/** 
* Encodes this array as a human readable JSON string for debugging, such 
* as: 
* <pre> 
* [ 
*  94043, 
*  90210 
* ]</pre> 
* 
* @param indentSpaces the number of spaces to indent for each level of 
*  nesting. 
*/ 
public String toString(int indentSpaces) throws JSONException { 
    JSONStringer stringer = new JSONStringer(indentSpaces); 
    writeTo(stringer); 
    return stringer.toString(); 
} 

的問題是,您需要將您ChecklistAnswer先轉換成JSON對象爲您JSONArray正常工作。

再從的Javadoc:

/** 
* A dense indexed sequence of values. Values may be any mix of 
* {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings, 
* Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}. 
* Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite() 
* infinities}, or of any type not listed here. 

...

1

以我ChecklistAnwer類別i中加入:

public JSONObject toJsonObject(){ 
    JSONObject json = new JSONObject(); 

    try { 
     json.put("questionId", questionId); 
     json.put("checklistId", checklistId); 
     json.put("answer", answer); 
     json.put("remark", remark); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return json; 
} 

在我的其他類:

JSONArray answers = new JSONArray(); 

ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(),   text.getText().toString()); 

answers.put(answer.toJsonObject()); 

如果我填充陣列:

String js = answers.toString(1); 

和回報:

[ 
{ 
    "answer": true, 
    "questionId": 1, 
    "remark": "", 
    "checklistId": 2 
}, 
{ 
    "answer": false, 
    "questionId": 4, 
    "remark": "teesxfgtfghyfj", 
    "checklistId": 2 
}, 
{ 
    "answer": true, 
    "questionId": 4, 
    "remark": "", 
    "checklistId": 2 
}, 
{ 
    "answer": true, 
    "questionId": 4, 
    "remark": "", 
    "checklistId": 2 
}, 
{ 
    "answer": true, 
    "questionId": 4, 
    "remark": "", 
    "checklistId": 2 
}, 
{ 
    "answer": true, 
    "questionId": 4, 
    "remark": "", 
    "checklistId": 2 
}, 
{ 
    "answer": true, 
    "questionId": 4, 
    "remark": "", 
    "checklistId": 2 
} 
] 

感謝@Selvin