JSONArray到java中的String

时间:2014-11-06 09:47:18

标签: java android arrays json string

我有JSONArray看起来像这样:

enter image description here

如何将其转换为字符串?

如果我尝试:

json.toString();

字符串是:

["package.package.ChecklistModels.ChecklistAnswer@405dddd8","package.package.ChecklistModels.ChecklistAnswer@405ddf48","package.package.ChecklistModels.ChecklistAnswer@405de070","package.package.ChecklistModels.ChecklistAnswer@405de198","package.package.ChecklistModels.ChecklistAnswer@405de2c0","package.package.ChecklistModels.ChecklistAnswer@405de3e8","package.package.ChecklistModels.ChecklistAnswer@405de510"]

但我想要这样的事情:

    {
 "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();

6 个答案:

答案 0 :(得分:1)

问题不在于@Selvin提到的JSONArray.toString()。

来自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 :(得分:1)

在我的ChecklistAnwer课程中,我添加了:

   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

答案 2 :(得分:0)

您不需要使用任何额外的库。 JSONArray类有an extra .toString(int) method,可以为您打印漂亮的打印。 int参数是缩进因子。

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

它也适用于JSONObject

更大的问题是你的JSONArray没有正确构建。你应该向它添加其他JSONArrayJSONObject,但是你要添加其他对象。他们隐含地变成了String。在将它们放入数组之前,您需要将它们转换为JSON。

答案 3 :(得分:0)

这对我来说很完美

String result = json.toString(4);

我试图将其写入文件,这很完美。

答案 4 :(得分:-1)

您可以使用gson库:https://code.google.com/p/google-gson/

Gson gson = new Gson();
String output = gson.toJson(object);

答案 5 :(得分:-1)

试试这个:

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