2014-11-06 79 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